Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将JSON数组转换为键值数组?_Javascript_Arrays_Json_Key_Nvd3.js - Fatal编程技术网

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

Javascript 如何将JSON数组转换为键值数组?,javascript,arrays,json,key,nvd3.js,Javascript,Arrays,Json,Key,Nvd3.js,我希望我问得对 我有一个数组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" } 我想要的是将以

我希望我问得对

我有一个数组
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绘制一个带有焦点图表的线条:

  //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数据=[{
“id”:“23”,
“估价人”:“129”,
“日期”:“2016-04-05T15:20:08.218+0100”
}, {
“id”:“24”,
“估价对象”:“131”,
“日期”:“2016-04-05T15:20:10.272+0100”
}]
var结果=data.map(函数(o){
返回{
钥匙:o.id,
价值观:{
x:o.valuee,
y:o.Date
}
}
});

document.write(“+JSON.stringify(result,0,4)+”)非常感谢!解决办法比我想象的要简单得多。