[英]JavaScript: Convert object structure
I have an object like this: 我有一个像这样的对象:
{
"A": [ "-4927","8779","-9971","-23767" ],
"B": [ "-10617","-1456","3131","259" ],
"C": [ "-5185","1168","21501","18989" ],
"D": [ "2010","5664","2148","-674" ]
}
I want to convert to this: 我想转换成这个:
[
{
name: 'A',
data: ["-4927","8779","-9971","-23767"]
}, {
name: 'B',
data: ["-10617","-1456","3131","259"]
}, {
name: 'C',
data: ["-5185","1168","21501","18989"]
}, {
name: 'D',
data: ["2010","5664","2148","-674"]
}
]
I have used this following method: 我使用了以下方法:
var newData = [];
$.each($.parseJSON(data), function(k, v) {
newData['name'] = k;
newData['data'] = v;
});
But it only store the last key pair value as newData. 但它只将最后一个密钥对值存储为newData。 That is
那是
name: 'D',
data: ["2010","5664","2148","-674"]
I understand that it overwrite the previous data and store only the last data it get. 据我所知,它会覆盖以前的数据并仅存储它获得的最后数据。 But i can't solve this problem.
但我无法解决这个问题。
Any Help ? 任何帮助?
You are assigning the properties name
and data
directly to newData
instead of pushing an object into it as an array: 您将属性
name
和data
直接分配给newData
而不是将对象作为数组推送到其中:
Change your loop to this: 将你的循环改为:
var newData = [];
$.each($.parseJSON(data), function(k, v) {
const myNewData = {
name: k,
data: v
}
newData.push(myNewData);
});
You can do this using map
你可以使用
map
来做到这一点
const oldData = { "A": [ "-4927","8779","-9971","-23767" ] , "B": [ "-10617","-1456","3131","259" ] , "C": [ "-5185","1168","21501","18989" ] , "D": [ "2010","5664","2148","-674" ] } const newShape = ([ name, data ]) => ({ name, data }) const newData = Object.entries(oldData).map(newShape) console.log(newData) // [ { name: "A", data: [ "-4927","8779","-9971","-23767" ] }, ... ]
You can write the lambda inline, if you wish, but it's generally better to write programs with reusable pieces 如果愿意,你可以编写lambda内联,但通常用可重用的部分编写程序会更好
const newData =
Object.entries(oldData).map(([ name, data ]) => ({ name, data }))
jQuery can map over objects using $.map – note the differing lambda jQuery可以使用$ .map映射对象 - 注意不同的lambda
const newData =
$.map(oldData, (data, name) => ({ name, data }))
You can use of Object.keys
and Array.map
to generate your array. 您可以使用
Object.keys
和Array.map
来生成数组。
It can be translated to this: 它可以翻译成这样:
for each key in my object data, create a new item inside of a new array.
对于我的对象数据中的每个键,在新数组中创建一个新项。 This item is going to be an object, with a key name and a key data.
此项目将成为一个对象,具有密钥名称和密钥数据。
const data = { A: ['-4927', '8779', '-9971', '-23767'], B: ['-10617', '-1456', '3131', '259'], C: ['-5185', '1168', '21501', '18989'], D: ['2010', '5664', '2148', '-674'], }; const ret = Object.keys(data).map(x => ({ name: x, data: data[x], })); console.log(ret);
You could use a for in
loop and push
each element. 您可以使用
for in
循环并push
每个元素。
var obj = { "A":["-4927","8779","-9971","-23767"], "B":["-10617","-1456","3131","259"], "C":["-5185","1168","21501","18989"], "D":["2010","5664","2148","-674"] } var newArr = []; for(p in obj){ newArr.push({"name":p,"data":obj[p]}) } console.log(newArr)
Should be: 应该:
var newData = [];
$.each($.parseJSON(data), function(k, v) {
newData.push({'name': k, 'data': v});
});
You also don't need to use jQuery. 您也不需要使用jQuery。 You can do the same in vanilla JS:
你可以在vanilla JS中做同样的事情:
var newData = [];
Object.keys(data).forEach(function(k) {
newData.push({'name': k, 'data': data[k]});
});
The following code will return your expected result. 以下代码将返回您的预期结果。
var newData = [];
for(var key in data) {
newData.push({
name: key,
data: data[key]
});
}
If we weren't using jQuery
, we could use a combination of Object.keys() and Array.prototype.map() 如果我们不使用
jQuery
,我们可以使用Object.keys()和Array.prototype.map()的组合
Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.
Object.keys()方法以与普通循环相同的顺序返回给定对象自己的属性名称的数组。
Object.keys(data)
would simply return ["A", "B", "C", "D"]
Object.keys(data)
只返回["A", "B", "C", "D"]
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。
In that map function callback we can return an object in the specific way we need our data formatted. 在该map函数回调中,我们可以以我们需要格式化数据的特定方式返回一个对象。
const data = { "A": ["-4927", "8779", "-9971", "-23767"], "B": ["-10617", "-1456", "3131", "259"], "C": ["-5185", "1168", "21501", "18989"], "D": ["2010", "5664", "2148", "-674"] } const newData = Object.keys(data).map(function(prop) { return { name: prop, data: data[prop] } }) console.log(newData)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.