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

196
Visualizações
Manipulate an array of objects inside another array

I'm having a problem manipulating an array of objects inside another array.I created an example to explain what I need. So here we have this array:

array1 =
[
    {
        "first": "A",
        "second": [
            "one"
        ]
    },
    {
        "first": "A",
        "second": [
            "three"
        ]
    },
    {
        "first": "C",
        "second": [
            "four"
        ]
    },
    {
        "first": "D",
        "second": [
            "one"
        ]
    },
    {
        "first": "C",
        "second": [
            "three"
        ]
    },
]

I've been trying to create a logic to manipulate this array and return something like this:

result = 
[
    {
        "first": "A",
        "second": [
            "one",
            "three"
        ]
    },
    {
        "first": "C",
        "second": [
            "three",
            "four"
        ]
    },
    {
        "first": "D",
        "second": [
            "one"
        ]
    },
]   
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

for(let i = 0, n = result.length; i < n; ++i) {

   for(let k = 0, l = result[i].second.length; k < l; ++k) {
  
     result[i].second.push("anything");
  
   }

 }
about 4 years ago · Juan Pablo Isaza Relatório

0

Could you try this ?

let result = [];

array1.forEach(obj => {
  if(!result[obj.first]) {
    result[obj.first] = {
      first: obj.first
      , second: []
    }
  }

  result[obj.first].second.push(obj.second[0])
})
about 4 years ago · Juan Pablo Isaza Relatório

0

You can go with the following approach:

array1 =
[
    {
        "first": "A",
        "second": [
            "one"
        ]
    },
    {
        "first": "A",
        "second": [
            "three"
        ]
    },
    {
        "first": "C",
        "second": [
            "four"
        ]
    },
    {
        "first": "D",
        "second": [
            "one"
        ]
    },
    {
        "first": "C",
        "second": [
            "three"
        ]
    },
]

var array2 = []

array1.forEach(function(item) {
  var existing = array2.filter(function(v, i) {
    return v.first == item.first;
  });
  if (existing.length) {
    var existingIndex = array2.indexOf(existing[0]);
    array2[existingIndex].second = array2[existingIndex].second.concat(item.second);
  } else {
    if (typeof item.second == 'string')
      item.second = [item.second];
    array2.push(item);
  }
});

console.log(array2);

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