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

146
Visualizações
Javascript: Merging Object Array

I have an array of objects with nested object, as shown below:

[
  {
    "metric1": 4.1,
    "addons": {
      "call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
      "ThisDN_1": "50002",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  },
  {
    "metric2": -12345.123412341234,
    "addons": {
      "call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
      "ThisDN_2": "50003",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  },
  {
    "metric3": -2345.123412341234,
    "addons": {
      "call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
      "ThisDN_1": "50002",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  },
  {
    "metric4": -345.12341234123414,
    "addons": {
      "call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
      "ThisDN_2": "50003",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  }
]

I want to merge some of the array objects with the same nested object addons, so that the previous array becomes:

[
  {
    "metric1": 4.1,
    "metric3": -2345.123412341234,
    "addons": {
      "call_uuid_1
": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
      "ThisDN_1": "50002",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  },
  {
    "metric2": -12345.123412341234,
    "metric4": -345.12341234123414,
    "addons": {
      "call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
      "ThisDN_2": "50003",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  }
]

Are there any ways to do this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use JSON.stringify to look for equality in the addons object and then merge your objects. Like this:

const array = [
  {
    "metric1": 4.1,
    "addons": {
      "call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
      "ThisDN_1": "50002",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  },
  {
    "metric2": -12345.123412341234,
    "addons": {
      "call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
      "ThisDN_2": "50003",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  },
  {
    "metric3": -2345.123412341234,
    "addons": {
      "call_uuid_1": "13d1e097-0363-4a81-b9e4-85c6a770cdb2",
      "ThisDN_1": "50002",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  },
  {
    "metric4": -345.12341234123414,
    "addons": {
      "call_uuid_2": "13d1e097-0363-4a81-b9e4-85c6a770cdb3",
      "ThisDN_2": "50003",
      "call_uuid": "13d1e097-0363-4a81-b9e4-85c6a770cdb4",
      "ThisDN": "50004"
    }
  }
];

const mergedArray = [];
const addonsToIndexMap = {};

array.forEach(item => {
 const stringifyAddon = JSON.stringify(item.addons);    
 let indexInTheMap = addonsToIndexMap[stringifyAddon];
 if(indexInTheMap !== undefined) {
   mergedArray[indexInTheMap] = {...mergedArray[indexInTheMap], ...item};
 } else {
   mergedArray.push(item);
   addonsToIndexMap[stringifyAddon] = mergedArray.length - 1;
 }
});
console.log(mergedArray);

The use of addonsToIndexMap is just for optimization purposes. Note: This method only works if the order of keys within the addon objects is the same. Also, I rectified the input in my program to update the key call_uuid_l with call_uuid_1 to get the expected output, as mentioned in the comments. Please modify it accordingly.

about 4 years ago · Juan Pablo Isaza Relatório

0

If the array has more than one element with the same addons object, you can delete the first one's addons object and then merge it with the others.

for (let i = 0; i < arr.length; i++) {
  for (let j = i + 1; j < arr.length; j++) {
    if (JSON.stringify(arr[i]["addons"]) === JSON.stringify(arr[j]["addons"])) {
      delete arr[i]["addons"];
      Object.assign(arr[i], arr[j]);
      arr.splice(j, 1);
      j -= 1;
    }
  }
}

Output:

[
  {
    metric1: 4.1,
    metric3: -2345.123412341234,
    addons: {
      call_uuid_1: '13d1e097-0363-4a81-b9e4-85c6a770cdb2',
      ThisDN_1: '50002',
      call_uuid: '13d1e097-0363-4a81-b9e4-85c6a770cdb4',
      ThisDN: '50004'
    }
  },
  {
    metric2: -12345.123412341234,
    metric4: -345.12341234123414,
    addons: {
      call_uuid_2: '13d1e097-0363-4a81-b9e4-85c6a770cdb3',
      ThisDN_2: '50003',
      call_uuid: '13d1e097-0363-4a81-b9e4-85c6a770cdb4',
      ThisDN: '50004'
    }
  }
]
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