Jquery, checking if a value exists in array or not

You can also do this by using .map() or just want to know how it works you can do it like this:

1
2
3
4
5
6
7
8
9
10
var added=false;
$.map(arr, function(elementOfArray, indexInArray) {
if (elementOfArray.id == productID) {
elementOfArray.price = productPrice;
added = true;
}
}
if (!added) {
arr.push({id: productID, price: productPrice})
}

Leave a Reply