0

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?

2
  • Yes, you can first check whether the key exists in an array of object. Commented Jan 23, 2017 at 5:37
  • stackoverflow.com/questions/1098040/… How to check Commented Jan 23, 2017 at 5:44

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.