一尘不染

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

javascript

给定一个JavaScript对象:

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

如何有效地将内部对象提取到数组中? 我不需要维护 object [n] ID 的句柄。

var dataArray = [
    {id: 1, name: "Fred"}, 
    {id: 2, name: "Wilma"}, 
    {id: 3, name: "Pebbles"}]

阅读 236

收藏
2020-05-01

共1个答案

一尘不染

var dataArray = [];
for(var o in dataObject) {
    dataArray.push(dataObject[o]);
}
2020-05-01