I have an array of objects in my code. The objects are having same keys with different or same values.
var a = [];
a.push({taxid : 1, tax_name: 'VAT', tax_value:'25.00'});
a.push({taxid : 2, tax_name: 'Service Tax', tax_value:'20.00'});
a.push({taxid : 1, tax_name: 'VAT', tax_value:'25.00'});
a.push({taxid : 2, tax_name: 'Service Tax', tax_value:'75.00'});
console.log(a);
The array Looks like below:
I want to iterate through this array and if the taxid is the same in the objects, then the tax_valueshould be summed up.
I tried using two for loops and it's working but in that case, it is checking the id with itself and summing up the value with itself. Like below:
var sum = 0;
var newArray = [];
for (var i=0; i<a.length;i++){
for (var j=0;j<a.length;j++){
if(a[i]['taxid']==a[j]['taxid']){
sum = a[i]['tax_value'] + a[j]['tax_value'];
}
}
newArray.push({taxid : a[i]['taxid'], tax_value : sum});
}
I appreciate your concerns, Thank You.

forloops.