Reputation: 53
I want to iterate through a JSON response, check to see if key,value exists and if not, add it to the array.
$scope.InitProdToArray = function (data) {
angular.forEach($scope.data.obj.Product, function(value, index) {
if (value.Product_type != 'T' ) {
$scope.data.obj.Product.push({Product_type: 'T'});
}
if (value.Product_type != '16364NB' ) {
$scope.data.obj.Product.push({Product_type: '16364NB'});
}
if (value.Product_type != '39087NB' ) {
$scope.data.obj.Product.push({Product_type: '39087NB'});
}
if (value.Product_type != 'C' ) {
$scope.data.obj.Product.push({Product_type: 'C'});
}
if (value.Product_type != '4NB' ) {
$scope.data.obj.Product.push({Product_type: '4NB'});
}
});
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"
}
]
This doesn't seem to work because I'm pushing the key,value pair every time it iterates through each node. I end up getting back a JSON that has multiple product_type that is the same.
Is there a way to stop the code from adding additional key,value if it already exists?
Upvotes: 0
Views: 130
Reputation: 756
var TYPES = { T: 'T', NB4: '4NB', C: 'C', NB39087: '39087NB' };
var products = [{ "Count": 28, "Product_type": "T" }, { "Count": 88, "Product_type": "4NB" }];
/* check if the element key is not the specified value....*/
function isKeyInElement(keyToCheck, element) {
return element.Product_type === keyToCheck;
}
/* checks if there are NO elements in array with the specified key ...*/
function isKeyInArray(keyToCheck, array) {
return !!array.filter(function (element) { return isKeyInElement(keyToCheck, element); }).length;
}
/* another way to check if there are NO elements in array with the specified key ...*/
function isKeyInArray2(keyToCheck, array) {
return array.map(function (element) { return element.Product_type; }).indexOf(keyToCheck) > -1;
}
function initProdToArray(source) {
if (!isKeyInArray(TYPES.T, source)) { source.push({ Product_type: TYPES.T }); }
if (!isKeyInArray(TYPES.NB4, source)) { source.push({ Product_type: TYPES.NB4 }); }
if (!isKeyInArray(TYPES.C, source)) { source.push({ Product_type: TYPES.C }); }
if (!isKeyInArray(TYPES.NB39087, source)) { source.push({ Product_type: TYPES.NB39087 }); }
}
initProdToArray(products);
console.log(products);
Upvotes: 0
Reputation: 7179
From your code it seems like you are doing search and insert rather than things to do with key/value.
If you are supporting new browsers only there is a handy function called find
for this purpose.
var productTypes = ['T', '16364NB', '39087NB', 'C', '4NB'];
angular.forEach(productTypes, function(value) {
var exists = $scope.data.obj.Product.find(function(item) {
return (item.Product_type == value);
});
if (!exists) {
$scope.data.obj.Product.push({
Product_type: value,
count: 0
});
}
});
If you need to support older browsers, you'll need to add polyfill for find
as well.
Upvotes: 0
Reputation: 375
You seem to have written your type check upside down.
instead of if (value.Product_type != 'T' ) {...
I would have imagine something like if (value.Product_type == 'T' ) {...
this way you would push to Product
array only when the type is matching.
apart from that, you can also check before pushing if you Product
array already contains a key of that type : if($scope.data.obj.Product.indexOf(value.Product_type)!==undefined)
Upvotes: 1