简体   繁体   English

如何将这个对象对象转换为对象数组

[英]How do I turn this object of objects into an array of objects

This is my object that contains 3 objects.这是我的对象,包含 3 个对象。

{
    "costco":
        {
            "costco_11":{"id":1, "address":"123 main"}, 
            "costco_12":{"id":2, "address":"345 Oak"},
            "costco_13":{"id":3, "address":"789 Birch"}
        }
}

I would like to create a single array that contains 3 objects so that I can dynamically create some ListView items in React-Native.我想创建一个包含 3 个对象的单个数组,以便我可以在 React-Native 中动态创建一些 ListView 项目。

I would like to avoid using Lodash(Probably not needed, but I just want to make sure since I mentioned react-native)我想避免使用 Lodash(可能不需要,但我只是想确定一下,因为我提到了 react-native)

I would like to create a single array that contains 3 objects我想创建一个包含 3 个对象的数组

Simply get Object.values只需获取Object.values

var obj = {
     "costco":{
         "costco_11":{"id":1, "address":"123 main"}, 
         "costco_12":{"id":2, "address":"345 Oak"},
         "costco_13":{"id":3, "address":"789 Birch"}
      }
};
obj.costco = Object.values(obj.costco);
console.log(obj);

There is no need for external libs;不需要外部库; you can do it in plain Javascript.你可以用普通的 Javascript 来完成。

let myObj =  {"costco":{
         "costco_11":{"id":1, "address":"123 main"}, 
         "costco_12":{"id":2, "address":"345 Oak"},
         "costco_13":{"id":3, "address":"789 Birch"}
      }
};

let myArray = Object.values(myObj.costco);

I made a little fiddle for this.我为此做了一点小提琴

const newArr = Object.keys(fakeOb).map(function(k) { return fakeOb[k]});
console.log(newArr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM