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?