gpt4 book ai didi

javascript - 将项目推送到 JSON 数组中

转载 作者:行者123 更新时间:2023-12-02 18:49:25 26 4
gpt4 key购买 nike

我有一个 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

最佳答案

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

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

关于javascript - 将项目推送到 JSON 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18488231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com