我有一个 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,我都有一些“解释”。我想添加另一个属性,以便拥有这样的东西:
[
{
"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