获取对象键数组我希望在jQuery或纯JavaScript中将JavaScript对象的键作为数组。有比这更少的方式吗?var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };var keys = [];for (var key in foo) {
keys.push(key);}
3 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
Object.keys
:
var foo = {
'alpha': 'puffin',
'beta': 'beagle'
};
var keys = Object.keys(foo);
console.log(keys) // ['alpha', 'beta']
// (or maybe some other order, keys are unordered).
Object.keys
胡说叔叔
TA贡献1804条经验 获得超8个赞
Object.keys()
Object.keys()
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };var keys = [];for (var key in foo) { keys.push(key);}
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };var keys = [], i = 0;for (keys[i++] in foo) {}
Object.keys()
添加回答
举报
0/150
提交
取消