admin管理员组文章数量:1394990
I currently have an array of objects that looks like this:
[
{
"key":"CES0000000001",
"25568.95655":"29923",
"25568.96078":"31603"
},
{
"key":"CES0000000001",
"25568.96501":"34480",
"25568.96924":"38347"
}
]
I'm trying to figure out the best way to restructure this data to look like this:
[
{
"key":"CES0000000002",
"values": [ [ 25568.95655 , 29923 ] , [ 25568.96078 , 31603 ] ]
},
{
"key":"CES0000000002",
"values": [ [ 25568.96501 , 34480 ] , [ 25568.96924 , 38347 ] ]
}
]
Can anyone provide some advice for this and any good resources for restructuring javascript objects? I'm getting more into visualization using d3.js and data formatting is key.
I currently have an array of objects that looks like this:
[
{
"key":"CES0000000001",
"25568.95655":"29923",
"25568.96078":"31603"
},
{
"key":"CES0000000001",
"25568.96501":"34480",
"25568.96924":"38347"
}
]
I'm trying to figure out the best way to restructure this data to look like this:
[
{
"key":"CES0000000002",
"values": [ [ 25568.95655 , 29923 ] , [ 25568.96078 , 31603 ] ]
},
{
"key":"CES0000000002",
"values": [ [ 25568.96501 , 34480 ] , [ 25568.96924 , 38347 ] ]
}
]
Can anyone provide some advice for this and any good resources for restructuring javascript objects? I'm getting more into visualization using d3.js and data formatting is key.
Share Improve this question edited Oct 28, 2014 at 23:55 thefreeline asked Oct 28, 2014 at 23:07 thefreelinethefreeline 6311 gold badge12 silver badges26 bronze badges 3-
1
Seems like all you need to do is loop over the array and create some now objects with a
key
andvalues
property. What specifically are you stuck on? – user1106925 Commented Oct 28, 2014 at 23:09 -
And where is the second object getting its
key
from? It's not represented in the original data. – user1106925 Commented Oct 28, 2014 at 23:11 - Corrected the typo with the second object key. I'm just not accustomed to working with arrays/objects and wasn't sure which methods I should be using here. iSchluff provided a straightforward example that gets the job done. Thanks! – thefreeline Commented Oct 28, 2014 at 23:58
2 Answers
Reset to default 6my solution would be
var arr= [
{
"key":"CES0000000001",
"25568.95655":"29923",
"25568.96078":"31603"
},
{
"25568.96501":"34480",
"25568.96924":"38347"
}
];
var transformed= arr.map(function(obj){
var result= {
key: obj.key,
values: []
}
for (var key in obj) {
if (obj.hasOwnProperty(key) && key !== "key") {
result.values.push([key, obj[key]]);
}
}
return result;
});
console.log(transformed);
This is an old post I see, but I want to share how you could do this with es6 object destructuring and restructuring. if you have an object...
const obj1 = {
"key":"CES0000000001",
"25568.95655":"29923",
"25568.96078":"31603"
};
you could have:
const restructure = ({key, ...values}) => ({key, Object.entries(values)});
const obj2 = restructure(obj1);
or for the array you could restructure all of them with:
const b = arr.map(({key, ...values}) => ({key, Object.entries(values)}));
or if you are reusing the function...
const b = arr.map(restructure);
本文标签: JavascriptRestructure array of objectsStack Overflow
版权声明:本文标题:Javascript - Restructure array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744105851a2591059.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论