Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Sum specific properties values of objects in array if they have same property name and return unique array as result using java script

my problem is to map two array of objects and sum their specifics properties if they have same name value.

For example if I would like to sum x and y of all objects where property name is 'v' etc. Here is my code

var arr = [{name:'v', x:1, b:2, c:3},{name:'r', x:2, b:0, c:3},{name:'v', x:4, b:3, c:3}, {name:'v', x:1, b:1, c:3}];
let arr2 = []
let obj = {name:null, x:null, b: null, c:null}
arr.map(item => {
for(let i=0; i<= arr.length; i++){

    if(item.name === arr[i].name){
     let a = arr.reduce((a, b) => ({x: a.x + b.x, b: a.b + b.b})); 
      obj.name = item.name, obj.x = a.x, obj.b = a.b, obj.c = item.c
    } else {
    obj.name = item.name, obj.x=item.x, obj.b=item.b, obj.c=item.c
        }
  arr2.push(obj)
        }
  }
)

console.log(arr2)

As result i would like to return array like this

arr2 = [{name: 'v', x: 6, b: 6, c: 3}, {name: 'r', x:2, b:0, c:1}]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

const sumProperties = (arr) => {
  const result = [];
  const map = new Map();
  for (const obj of arr) {
    const key = obj.name;
    if (!map.has(key)) {
      map.set(key, obj);
    } else {
      const oldObj = map.get(key);
      const newObj = { ...oldObj, ...obj };
      map.set(key, newObj);
    }
  }
  map.forEach((value) => {
    result.push(value);
  });
  return result;
}
console.log(sumProperties(arr));
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is the answer for my question

var arr = [{v:'v', x:1, b:2, c:3},{v:'r', x:2, b:0, c:3},{v:'v', x:4, b:3, c:3},{v:'v', x:10, b:3, c:3},{v:'r', x:1, b:1, c:3},{v:'v', x:11, b:2, c:3},{v:'r', x:22, b:0, c:3}];
let arr2 = []
for(let j=0; j< arr.length;j++){
    for(let i=j+1; i< arr.length; i++){
        let obj = {v:null, x:null, b: null, c:null}
        if(arr[j].v === arr[i].v ){
          let a =  arr.filter(({v})=> v === arr[j].v).reduce((a, b) => ({x: a.x + b.x, b: a.b + b.b})); 
        obj.v = arr[j].v
        obj.x = a.x 
        obj.b = a.b 
        obj.c = arr[j].c  
        i= arr.length
        console.log("DA", obj)
        if(!arr2.some(item=> item.v === obj.v)){
            arr2.push(obj)
        }
    } else if(i+1 === arr.length){  
        obj.v = arr[j].v 
        obj.x = arr[j].x 
        obj.b = arr[j].b 
        obj.c = arr[j].c
        console.log("NE", obj)
        if(!arr2.some(item=> item.v === obj.v)){
            arr2.push(obj)
            }
        }
    }
}
console.log(arr2, "REZ")

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda