繁体   English   中英

如何将JSON数组转换为键值数组?

[英]How to convert a JSON array to a key value array?

我希望我正确地问了这个。

我有一个数组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"}]

我该怎么做? 非常感谢。

您可以按照以下方式进行操作

notes.map((note) => {
    return {
        key: note.id,
        values: [{
            x: note.valuee,
            y: note.datee
        }]
    } 
})

您可以使用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>"); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM