【问题标题】:Remove Column name from JSON从 JSON 中删除列名
【发布时间】:2021-08-07 04:09:37
【问题描述】:

我是 Javascript 新手,正在尝试从 my_result 对象获取以下输出。我尝试使用地图功能,但无法获得所需的输出。

const my_result = '[{"result":true, "count":42}, {"result":false, "count":30}]'; 
//Output
[ [ true, 42 ], [false, 30] ]

我在这里找到了类似的示例,但我无法弄清楚如何对多行计数执行类似的操作。 (Remove Column name from array in JSON)

my_result 可以根据我从中提取数据的表具有可变的行数。

【问题讨论】:

  • 您希望它作为一个对象数组、([ { 'true': 42 }, { 'false': 30 } ]) 一个数组数组 ([ [ true, 42 ], [false, 30] ]) 或作为单个对象 ({ 'true': 42, 'false': 30 })。现在您的输出不是有效的 JavaScript。
  • 抱歉现在编辑了帖子,我想要它作为数组的数组。谢谢!

标签: javascript node.js arrays json object


【解决方案1】:

const my_result = '[{"result":true, "count":42}, {"result":false, "count":30}]'; 

const res = JSON.parse(my_result).map(Object.values)

console.log(res)

【讨论】:

  • 如果你想要一个字段的子集或者只是为了更明确,你可以改用.map(e => [e.result, e.count])
【解决方案2】:

地图效果很好。

const my_result = '[{"result":true, "count":42}, {"result":false, "count":30}]';

const formatted = JSON.parse(my_result).map(o => ({
  [o.result]: o.count
}));

console.log(formatted);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2020-01-24
    相关资源
    最近更新 更多