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

194
Views
Expand a nested array to make a flat JS Map?

Say I have a JS Map like this:

[
    {
        id: 1,
        name: "Foo",
        contents: [1,2,3],
        morecontents: ["a","b"],
    },
    {
        id: 2,
        name: "Bar",
        contents: [4,5,6]
    }
]

I want to be able to expand the arrays within it to produce a Map like this:

{
   {
      "id":1,
      "name":"Foo",
      "contents":1,
      "morecontents":"a"
   },
   {
      "id":1,
      "name":"Foo",
      "contents":2,
      "morecontents":"b"
   },
   {
      "id":1,
      "name":"Foo",
      "contents":3,
      "morecontents":""
   }
} 
[...]

Note the arrays will always be at expected keys, and there may be multiple arrays to expand. In the event arrays are different lengths (very likely) the fields can be left blank.

How could this be achieved in Node.JS/vanilla JS?

Thank you.

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

0

array.map(obj =>
    obj.contents.map((content,i) =>
        new Object({
            ...obj,
            contents: content,
            morecontents: obj.morecontents[i] ? obj.morecontents[i] : ""  
        })
).flat()
about 4 years ago · Juan Pablo Isaza Report

0

Considering the arrays within the object are dynamic and anyone of these arrays could be of longer length a following solution will be more comprehensive. First it finds the arrays elements within the object, then finds the array with most elements, then it creates copies of object equal to the number of maximum lengths of its child array and lastly updates the elements according to the index of these copies. The results are then flattened into a single result.

            array.map((obj, oi) => {
                const arrayNames = Object.keys(obj).filter(k => Array.isArray(obj[k]));
                let maxlen = arrayNames.map(a => obj[a].length).reduce((p, c) => p > c ? p : c);
                const res = new Array(maxlen).fill({}).map(objn => Object.assign({}, objn, obj));
                res.forEach((x, i) => arrayNames.forEach(name => x[name] = i < obj[name].length ? obj[name][i] : ""));
                return res;
            }).flat()
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!