首页 > 解决方案 > 在 JavaScript 中将 JSON 对象转换为数组

问题描述

我从第三方 api 获取 JSON 对象。我正在尝试将其转换为对象数组。

key: {
  id1: {key:value},
  id2: {key:value},
...

}

arrayName: [
     {key:id1, Key:value},
     {key:id1, Key:value}]

如果完全可以理解,希望我的描述。任何帮助,将不胜感激

标签: javascriptarraysjsonreact-native

解决方案


这可能会有所帮助

const a = {key: {
  id1: {key:"1"},
  id2: {key:"2"}
}}

const res = Object.entries(a.key).map(([k,v]) => {return {key:k, Key: v.key}});

console.log(res); // your desired result

在这里查看演示


推荐阅读