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

249
Visualizações
Getting an array from array of array objects

I have an array which consists of an array objects as shown:

dataArr = [
    {
       id: 1,
       arrObj: [
          {
             id: 11,
             label: 'apple'
          },
          {
             id: 12,
             label: 'ball'
          }
       ]
    },
    {
       id: 2,
       arrObj: [
          {
             id: 21,
             label: 'car'
          },
          {
             id: 22,
             label: 'dog'
          }
       ]
    }
];

I need to extract an array consisting of only arrObj objects:

var newArr = [
    {
         id: 11,
         label: 'apple'
      },
      {
         id: 12,
         label: 'ball'
      },
      {
         id: 21,
         label: 'car'
      },
      {
         id: 22,
         label: 'dog'
      }
];

Tried using reduce method unsuccessfully:

 dataArr.reduce((previousValue, currentValue, currentIndex, array) => {
     return previousValue. arrObj.concat(currentValue.arrObj)
 });

Let me know how to do this. Thanks

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

0

let dataArr = [
    {
       id: 1,
       arrObj: [
          {
             id: 11,
             label: 'apple'
          },
          {
             id: 12,
             label: 'ball'
          }
       ]
    },
    {
       id: 2,
       arrObj: [
          {
             id: 21,
             label: 'car'
          },
          {
             id: 22,
             label: 'dog'
          }
       ]
    }
];

let result = dataArr.flatMap(e => e.arrObj)
console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

You were pretty close.

  1. There's no arrObj property in your result, it's just an array.
  2. You need to provide an empty array as the initial value argument to reduce().

const dataArr = [{
    id: 1,
    arrObj: [{
        id: 11,
        label: 'apple'
      },
      {
        id: 12,
        label: 'ball'
      }
    ]
  },
  {
    id: 2,
    arrObj: [{
        id: 21,
        label: 'car'
      },
      {
        id: 22,
        label: 'dog'
      }
    ]
  }
];

const newArr = dataArr.reduce((previousValue, currentValue) => {
  return previousValue.concat(currentValue.arrObj)
}, []);

console.log(newArr);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use in one line by using Spread Operator...

dataArr.reduce((previousValue, currentValue) => [...previousValue?.arrObj, ...currentValue?.arrObj]);

Tip: use Optional chaining ?. in case there is no property arrObj!

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