在javascript数组中添加新列

Ash*_*ish 4 javascript arrays

我有一个包含三列的数组,如下所示:

data.push({
  country: new Date(),
    newSales: Math.random() * 1000,
     expenses: Math.random() * 5000
 });
Run Code Online (Sandbox Code Playgroud)

现在,点击按钮,我想在其中添加一个新列.谁能让我知道我们怎么做?

小智 9

您可以迭代数据数组并为每个元素添加键和值.

data[0]["foo"] = bar; // this can be useful if the key is not constant
Run Code Online (Sandbox Code Playgroud)

要么

data[0].foo = "bar"
Run Code Online (Sandbox Code Playgroud)