简体   繁体   English

从嵌套的 json 值对象中获取数组

[英]Get array from nested json value objects

I've been searching an answer for that but didn't found it.我一直在寻找答案,但没有找到。

I have an array like:我有一个数组,如:

const data2 = [{
    "abc":{
            companyCity:"Cupertino",
            conpanyName:"Apple"
        }
    },
    {
    "def":{
            companyCity:"Mountain View",
            conpanyName:"Google"
        }
    }
]  

And I'd like to convert to and array like omiting the parent keys:我想转换为和数组一样省略父键:

const data3 = [
    {
        companyCity:"Cupertino",
        companyName:"Apple",
    },
    {
        companyCity:"Mountain View",
        companyName:"Google"
    }
]

Perhaps, libraries like lodash have a method to achieve that, but didn't find it.也许,像 lodash 这样的库有一种方法可以实现这一点,但没有找到。 Any help would be very appreciated :)任何帮助将不胜感激:)

Iterate the array with Array.flatMap() (or lodash's _.flatMap() ), and get the an the inner object of each item using Object.values() (or _.values() ):迭代与阵列Array.flatMap()或lodash的_.flatMap()并获得每个项目的使用的内部对象Object.values()_.values()

 const data = [{"abc":{"companyCity":"Cupertino","conpanyName":"Apple"}},{"def":{"companyCity":"Mountain View","conpanyName":"Google"}}] const result = data.flatMap(Object.values) console.log(result)

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

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