从嵌套的json值对象获取数组 [英] Get array from nested json value objects
本文介绍了从嵌套的json值对象获取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在寻找答案,但没有找到.
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数组,就像省略父键一样:
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"
}
]
也许,像lodash这样的库有一种方法可以实现这一点,但是没有找到它.任何帮助将不胜感激:)
Perhaps, libraries like lodash have a method to achieve that, but didn't find it. Any help would be very appreciated :)
推荐答案
使用 Array.flatMap()
(或lodash的 _.flatMap()
)迭代数组,并使用 Object.values()
(或 _.values()
)获取每个项目的内部对象:
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()
):
const data = [{"abc":{"companyCity":"Cupertino","conpanyName":"Apple"}},{"def":{"companyCity":"Mountain View","conpanyName":"Google"}}]
const result = data.flatMap(Object.values)
console.log(result)
这篇关于从嵌套的json值对象获取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文