在多维数组中推送 属性

push property in multidimensional array

我想将 属性 压入多维数组。 在这段代码中,我得到 TypeError: myArr.second[i].push is not a function...

var myArr = {
"main": 2000,
"second": [
{
  "step1": 10,
  "step2": "lorem ipsum",
  "step3": "bla, bla",
  },
  {
  "step1": 20,
  "step2": "TEXT, TEXT",
  "step3": "bla, bla, bla",
}]
};


for(i=0; i < myArr.second.length; i++){
  var toPush =  {};
  toPush["step4"] = "text";
  myArr["second"][i].push(toPush);
}

有人可以帮助我吗?

使用dot notation or bracket notation定义属性。

for(i=0; i < myArr.second.length; i++){
  myArr["second"][i].step4 = "text";
}


或者您可以使用 Object.assign 方法从另一个对象复制属性。

for(i=0; i < myArr.second.length; i++){
  var toPush =  {};
  toPush["step4"] = "text";
  Object.assign(myArr["second"][i], toPush);
}