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

195
Views
Combine All Array Inside of Arrays

I have a problem on combining arrays that I wanted to get that's coming from another array.

datas

   datas = [
      {
        "id": "22333",
        "name": "",
        "details": [
          {
            "value": 1
          },
          {
            "value": 2
          }
        ]
      },
      {
        "id": "4444",
        "name": "",
        "details": [
          {
            "value": 99
          },
          {
            "value": 66
          }
        ]
      }
    ]

expected output

final = [
    {
      "value": 1
    },
    {
      "value": 2
    },
    {
        "value": 99
    },
    {
        "value": 66
    }
  ]

Code

  datas.map((data) => ({ ...data.details}))
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Replace map with flatMap.

The flatMap method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map followed by a flat of depth 1, but slightly more efficient than calling those two methods separately.

const datas=[{id:"22333",name:"",details:[{value:1},{value:2}]},{id:"4444",name:"",details:[{value:99},{value:66}]}];
    
const flattened = datas.flatMap(obj => obj.details);

console.log(flattened);

To get the actual numbers map over the details array.

const datas=[{id:"22333",name:"",details:[{value:1},{value:2}]},{id:"4444",name:"",details:[{value:99},{value:66}]}];
    
const flattened = datas.flatMap(obj => {
  return obj.details.map(el => el.value);
});

console.log(flattened);

about 4 years ago · Juan Pablo Isaza Report

0

datas.reduce((acc, val) => {
  return acc.concat(val.details)
}, [])
about 4 years ago · Juan Pablo Isaza Report

0

You can also make use of reduce here:

arr.reduce((acc, curr) => {
  acc.push(...curr.details);
  return acc;
}, [])

const arr = (datas = [
  {
    id: "22333",
    name: "",
    details: [
      {
        value: 1,
      },
      {
        value: 2,
      },
    ],
  },
  {
    id: "4444",
    name: "",
    details: [
      {
        value: 99,
      },
      {
        value: 66,
      },
    ],
  },
]);

const result = arr.reduce((acc, curr) => {
  acc.push(...curr.details);
  return acc;
}, []);

console.log(result);

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!