如何将JavaScript对象的属性值提取到数组中?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (43)

给定一个JavaScript对象:

var dataObject = {
   object1: {id: 1, name: "Fred"}, 
   object2: {id: 2, name: "Wilma"}, 
   object3: {id: 3, name: "Pebbles"}
};

如何有效地将内部对象提取到数组中?

var dataArray = [
    {id: 1, name: "Fred"}, 
    {id: 2, name: "Wilma"}, 
    {id: 3, name: "Pebbles"}]
提问于
用户回答回答于
var dataArray = new Array;
for(var o in dataObject) {
    dataArray.push(dataObject[o]);
}
用户回答回答于
var dataArray = Object.keys(dataObject).map(function(k){return dataObject[k]});

扫码关注云+社区