Get array of object's keys

前端 未结 7 1212
死守一世寂寞
死守一世寂寞 2020-11-22 05:41

I would like to get the keys of a JavaScript object as an array, either in jQuery or pure JavaScript.

Is there a less verbose way than this?

var foo          


        
7条回答
  •  死守一世寂寞
    2020-11-22 05:59

    I don't know about less verbose but I was inspired to coerce the following onto one line by the one-liner request, don't know how Pythonic it is though ;)

    var keys = (function(o){var ks=[]; for(var k in o) ks.push(k); return ks})(foo);
    

提交回复
热议问题