I am trying to check to see if a key,value pair in my array is found in the response JSON and if it is not, then I want to add it to the JSON.
我试图检查我的数组中的键,值对是否在响应JSON中找到,如果不是,那么我想将它添加到JSON。
$scope.myArray = [{
"Product_type": "T"
}, {
"Product_type": "4NB"
}, {
"Product_type": "C"
}, {
"Product_type": "39087NB"
}, {
"Product_type": "16364NB"
}];
JSON response array:
JSON响应数组:
$scope.data.obj.Product = [{
"Count": 28,
"Product_type": "T"
}, {
"Count": 88,
"Product_type": "4NB"
}, {
"Count": 20,
"Product_type": "C"
}, {
"Count": 3,
"Product_type": "39087NB"
}]
In this case, the missing key,value pair would be "Product_type": "16364NB". How would I do the check and then add to the response JSON?
在这种情况下,缺失的键,值对将是“Product_type”:“16364NB”。我如何进行检查然后添加到响应JSON?
Should I iterate through the JSON and then somehow reference myArray to figure out which one is missing, then push it to the JSON?
我是否应该遍历JSON,然后以某种方式引用myArray来确定缺少哪一个,然后将其推送到JSON?
1 个解决方案
#1
1
$scope.myArray.forEach(function (a, i) {
if ($scope.data.obj.Product.find(function(item) { return item.Product_type === a.Product_type }) === undefined) {
$scope.data.obj.Product.push(a);
}
});
#1
1
$scope.myArray.forEach(function (a, i) {
if ($scope.data.obj.Product.find(function(item) { return item.Product_type === a.Product_type }) === undefined) {
$scope.data.obj.Product.push(a);
}
});