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

202
Visualizações
How to merge elements in objects inside of array and returned unique objects with updated data

my question is how I can merge elements in array of objects and return array with unique objects. I was trying to use reduce but it's looks like I missing something and it's time to ask for hint. So how you can see I need merge elements of boolean where false is default value and true is new data, and return unique objects based on name. Thank you for help.

const arr = [
    {
      code: 'test1',
      name: 'TEST1',
      url: '/test1',
      elements: {
        option1: true,
        option2: false,
        option3: false,
        option4: false,
        option5: false
      }
    },
      {
        code: 'test1',
        name: 'TEST1',
        url: '/test1',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
        {
        code: 'test1',
        name: 'TEST1',
        url: '/test1',
        elements: {
          option1: false,
          option2: false,
          option3: false,
          option4: true,
          option5: false
      }
      },
       {
        code: 'test2',
        name: 'TEST2',
        url: '/test2',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
    ]

and output it should be:

const arr = [
    {
      code: 'test1',
      name: 'TEST1',
      url: '/test1',
      elements: {
        option1: true,
        option2: false,
        option3: true,
        option4: true,
        option5: false
      }
    },
       {
        code: 'test2',
        name: 'TEST2',
        url: '/test2',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
    ]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Working on the assumption that elements is the only thing that actually varies here, and that you can't switch back from false to true (since that what it seems like you are going for), you'd need to iterate over the key-value pairs in elements and set it to the originalValue || newValue, so something like this:

const arr = [
    {
      code: 'test1',
      name: 'TEST1',
      url: '/test1',
      elements: {
        option1: true,
        option2: false,
        option3: false,
        option4: false,
        option5: false
      }
    },
      {
        code: 'test1',
        name: 'TEST1',
        url: '/test1',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
        {
        code: 'test1',
        name: 'TEST1',
        url: '/test1',
        elements: {
          option1: false,
          option2: false,
          option3: false,
          option4: true,
          option5: false
      }
      },
       {
        code: 'test2',
        name: 'TEST2',
        url: '/test2',
        elements: {
          option1: false,
          option2: false,
          option3: true,
          option4: false,
          option5: false
      }
      },
    ].reduce((carry, current) => {
        const existing = carry.find(item => item.code === current.code);
        if (!existing) {
            carry.push(current);
        } else {
            Object.entries(current.elements).forEach(([k, v]) => {
                existing.elements[k] = existing.elements[k] || current.elements[k];
            });
        }
        return carry;
    }, []);
    
    console.log(arr);

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