Javascript 将项目推送到JSON数组中

Javascript 将项目推送到JSON数组中,javascript,arrays,json,angularjs,Javascript,Arrays,Json,Angularjs,我有一个JavaScript函数,它对API进行ajax调用,并获取JSON数组 下面是我得到的一个数组示例: [ { "ErrorType": "Errors", "Explanations": [ { "Explanation": "Price Missing", "Locations": [ 25, 45 ] }, { "Expla

我有一个JavaScript函数,它对API进行ajax调用,并获取JSON数组

下面是我得到的一个数组示例:

[
  {
    "ErrorType": "Errors",
    "Explanations": [
      {
        "Explanation": "Price Missing",
        "Locations": [
          25,
          45
        ]
      },
      {
        "Explanation": "Unit of measurement not valid",
        "Locations": [
          25,
          301,
          302
        ]
      }
    ]
  },
  {
    "ErrorType": "Warnings",
    "Explanations": [
      {
        Blablabla,
        Ithinkthere's already much here
      }
    ]
  }
]
我将其放入JavaScript数组中:

$scope.CorrectionDatas = ResponseFromApi;
因此,对于每种错误类型,我都有一些“解释”。我想添加另一个属性,以便拥有类似的内容:

[
  {
    "ErrorType": "Errors",
    "Explanations": [
      {
        "Explanation": "Price Missing",
        "Locations": [
          25,
          45
        ]
      },
      {
        "Explanation": "Unit of measurement not valid",
        "Locations": [
          25,
          301,
          302
        ]
      }
    ],
    "show": true
  },
  {
    "ErrorType": "Warnings",
    "Explanations": [
      {
        Blablabla,
        Ithinkthere's already much here 
      }
     ],
    "show":true
  }
]
我想我只能这样做:

$scope.CorrectionDatas.forEach(function (error) {
    error.push({ show: true });
});
但调试器给了我一个错误:

Error: error.push is not a function 
$scope.getErrors/</<@http://localhost:1771/dependencies/local/js/Correction/CorrectionCtrl.js:26
错误:Error.push不是一个函数
$scope.getErrors/请尝试以下方法:

$scope.CorrectionDatas.forEach(function (error){
     error["show"] = true;
});

每个错误都是一个对象,因此它没有push,代码应该是:

        $scope.CorrectionDatas.forEach(function(error) {
            error.show = true;
        });

我相信您遇到的问题是
error
不是数组,而是对象。这可以通过记录
typeof error
的输出来确认。如果是这种情况,则必须明确定义对象的
show
属性,如下所示:

$scope.CorrectionDatas.forEach(function (error){
    error['show'] = true; // alternatively, error.show = true;   
});

问题在于,虽然JSON响应构成一个数组,但该数组的每个元素都是一个对象,而不是数组。也就是说,
{“ErrorType”:“警告”,“解释”:[{Blablabla,我想这里已经有很多了}}
是一个对象……你能显示“typeof error”的输出吗?我相信这是一个对象,而不是数组。据说,我收到了一个对象,谢谢你的帮助