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

编程入门 行业动态 更新时间:2024-10-24 18:26:24
本文介绍了从嵌套的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值对象获取数组

本文发布于:2023-10-27 03:30:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数组   对象   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!