Js deals with data algorithm problems

var a = [

]
{x: 1, y: 2, z: 3},
{x: 1, y: 2, z: 4},
{x: 1, y: 1, z: 0},
{x: 1, y: 2, z: 3}

]
when the values of y in array an are the same as the sum of the same value z

example: a has three y with the same z sum of 3"4"3? Find the optimal solution

Nov.19,2021

var a = [
{x: 1, y: 2, z: 3},
{x: 1, y: 2, z: 4},
{x: 1, y: 1, z: 1},
{x: 1, y: 2, z: 3},
{x: 1, y: 1, z: 4},
]
for(var i=0,arr=[],cache=[],val=0;i<a.length;iPP){
  var y= a[i].y, 
   idx = cache.indexOf(y);
  (idx==-1)?(cache.push(y),arr.push({k:y,val:a[i].z})):(arr[idx].val+=a[i].z);
}
console.log(arr);
Menu