我希望我能正确地问这个问题。
我有一个数组notes
,其中每个元素都是JSON行。例如:
//notes[0] contains this line
{
"id":"23",
"valuee":"129",
"datee":"2016-04-05T15:20:08.218+0100"
}
//notes[1] contains this line:
{
"id":"24",
"valuee":"131",
"datee":"2016-04-05T15:20:10.272+0100"
}
我想要的是将之前的数组转换为这样的类型,所以我可以使用它来绘制带有nvd3的linewithfocus图表:
//notes[0] contains this line
{
key:"23",
values:[{x:"129",y:"2016-04-05T15:20:08.218+0100"}]
//notes[1] contains this line:
{
key:"24",
values:[{x:"131",y:"2016-04-05T15:20:10.272+0100"}]
我该怎么办?非常感谢你。
答案 0 :(得分:3)
您可以按照以下方式执行此操作
notes.map((note) => {
return {
key: note.id,
values: [{
x: note.valuee,
y: note.datee
}]
}
})
答案 1 :(得分:2)
您可以使用Array.map
var data = [{
"id": "23",
"valuee": "129",
"datee": "2016-04-05T15:20:08.218+0100"
}, {
"id": "24",
"valuee": "131",
"datee": "2016-04-05T15:20:10.272+0100"
}]
var result = data.map(function(o) {
return {
key: o.id,
values: {
x: o.valuee,
y: o.datee
}
}
});
document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>");
&#13;