如何更改javascript中的json结构? [英] how to change json structure in javascript?
本文介绍了如何更改javascript中的json结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下的json对象.
I have a json Object like following.
{987: "sqa", 988: "Squad"}
我需要像下面那样更改结构
I need to change the structure like following
[{id:987, value:'sqa'}, {id:988, value:'Squad'}]
如何使用JavaScript做到这一点?
How to do this with Javascript?
推荐答案
您可以获取对象的条目并将新对象映射到数组中.
You could get the entries of the object and map new objects in an array.
技术:
-
Object.entries
用于从数组中的对象获取所有键/值对,
Object.entries
for getting all key/value pairs from an object in an array,
Array#map
,用于为数组的每个项目映射一个新项目,
Array#map
, for mapping a new item for each item of the array,
解构分配键/值对和
简写属性用于将名称作为键并将值作为对象的值.
short hand properties for taking the name as key and the value as value for an object.
var object = { 987: "sqa", 988: "Squad" },
result = Object.entries(object).map(([id, value]) => ({ id, value }));
console.log(result);
这篇关于如何更改javascript中的json结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文