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

85
Views
Flatten inner array but assign outer properties

I have an input like this:

var input = [
    {
        "inner_array": [
            {
                "inner_data": "inner_data1"
            },
            {
                "inner_data": "inner_data2"
            }
        ],
        "outer_data": "outer_data",
    }
];

And I'd like to process it so it becomes this.

var output = [
    {
        "inner_data": "inner_data1",
        "outer_data": "outer_data",
    },
    {
        "inner_data": "inner_data2",
        "outer_data": "outer_data",
    }
];

In words: I'd like to flatten an inner array, while still keeping the outer array's properties. Does this have an easy solution (with built in lambda array functions), or should I write a function myself which handles this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use .flatMap() on your input array to loop over each object, along with an inner .map() to map each inner_array to a new object. The new object can be a combination of the outer_data value along with the inner_data value. The .flatMap() method will then merge all returned objects from the inner .map() calls for each object within input into one resulting array:

const input = [{ "inner_array": [{ "inner_data": "inner_data1" }, { "inner_data": "inner_data2" } ], "outer_data": "outer_data", }];

const res = input.flatMap(obj => obj.inner_array.map(inner => ({
  ...inner,
  outer_data: obj.outer_data
})));
console.log(res);

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!