Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda