【问题标题】:I want to Convert JSON to Array in JavaScript [closed]我想在 JavaScript 中将 JSON 转换为数组 [关闭]
【发布时间】:2019-10-25 22:24:26
【问题描述】:

我有一个 JSON 如下:

{
  "83" : {
    "firstColumn" : 78,
    "secondColumn" : 76,
    "thirdColumn" : 51
  },
  "390" : {
    "firstColumn" : 48,
    "secondColumn" : 25,
    "thirdColumn" : 45
  },
  "454" : {
    "firstColumn" : 96,
    "secondColumn" : 55,
    "thirdColumn" : 65
  },
  "524" : {
    "firstColumn" : 0,
    "secondColumn" : 23,
    "thirdColumn" : 18
  }
}

我想把它转换成这样的数组:

[[78,76,51,],[48,25,45],...[0,23,18]]

【问题讨论】:

  • 83390 等键应该如何处理?
  • 什么数组?有多少个维度?

标签: javascript arrays json


【解决方案1】:

可以使用Object.values()将其转换为无孔数组,并按照数字键值排序,然后映射数组,得到每个对象的Object.values()

const obj = { "83" : { "firstColumn" : 78, "secondColumn" : 76, "thirdColumn" : 51 }, "390" : { "firstColumn" : 48, "secondColumn" : 25, "thirdColumn" : 45 }, "454" : { "firstColumn" : 96, "secondColumn" : 55, "thirdColumn" : 65 }, "524" : { "firstColumn" : 0, "secondColumn" : 23, "thirdColumn" : 18 } }

const array = Object.values(obj).map(Object.values)

console.log(array)

【讨论】:

  • 我希望我的数组看起来像:[[78,76,51,],[48,25,45],...]
  • 查看更新的答案。下次包括预期的最终结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
相关资源
最近更新 更多