I am trying to check if there is an id existing in an array, if the id is already there, then I will update that id's value. Otherwise I will push the id and its value into the array. How can I achieve that with jquery?
I have tried but it doesn't work, but double the size of the array
$(itemData).each(function() {
var name = $(this).data("name");
var value = parseFloat($(this).data("amount"));
if(dataArr.length == 0) {
dataArr.push([name, value]);
} else {
$.each(dataArr, function(n, v) {
if(name == n) {
v += value;
}else {
dataArr.push([name, value]);
}
});
}
});