2016-04-13 8 views
-1

私はこれを正しく聞いて欲しいです。JSON配列をキー値配列に変換するにはどうすればよいですか?

私は各要素がJSON行である配列notesを持っています。ですから、例えば:

//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"}] 

?どうもありがとうございます。

答えて

2

あなたは次のように

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

を使用することができますありがとうございました!解決策は私が思っていたものよりも簡単でした。 –

1

これを行うことができますあなたは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>");

関連する問題