简体   繁体   English

向现有数组中添加新的key:value对

[英]Add new key:value pairs to existing an existing array

Here's my existing data: 这是我现有的数据:

var nodes = [
    {i: 0, radius: 18.637872483796723},
    {i: 0, radius: 17.174333481950903},
    {i: 0, radius: 13.194447610161163},
    {i: 1, radius: 8.000718059188364},
    {i: 1, radius: 4.08204211857112}
  ];

I need to add one new key:value pairs for each item in the list. 我需要为列表中的每个项目添加一个新的key:value对。 It is dynamically calculated: 它是动态计算的:

cx: i*2

So the new array would look like this: 因此,新数组如下所示:

[
  {i: 0, radius: 18.637872483796723, cx: 0},
  {i: 0, radius: 17.174333481950903, cx: 0},
  {i: 0, radius: 13.194447610161163, cx: 0},
  {i: 1, radius: 8.000718059188364, cx: 2},
  {i: 1, radius: 4.08204211857112, cx: 2}
];

I am a beginner, so excuse any terminology that I've butchered (I'm not sure if I'm using the term array correctly). 我是一个初学者,所以请原谅我所使用的任何术语(不确定是否正确使用了术语数组)。

nodes.forEach(function(n) {n.cx = n.i * 2});
for(var i=0; i<nodes.lenght;i++)
    nodes[i].cx = i*2;

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

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