Get array of object's keys

前端 未结 7 1181
死守一世寂寞
死守一世寂寞 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 06:05

    If you decide to use Underscore.js you better do

    var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };
    var keys = [];
    _.each( foo, function( val, key ) {
        keys.push(key);
    });
    console.log(keys);
    

提交回复
热议问题