我一直在寻找答案,但没有找到。

我有一个像这样的数组:

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


我想转换为和数组,就像省略父键一样:

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


也许,像lodash这样的库有一种方法可以实现这一点,但是没有找到它。任何帮助将不胜感激:)

最佳答案

使用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)

08-19 16:44