Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!