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

109
Views
Merge nested arrays into unique objects

How can I turn a nestedArray into a array of objects based on the values in the nested array? Here is an example:

const nestedArray = [{
id: "Degree",
options: ["HighSchool", "Undergraduate", "Bachelor", "Master", "Doctor"]
}, {
id: "gender",
options: ["male", "female"]
}]

const arrayOfObjects = []

nestedArray[0].options.map((degree) => {
nestedArray[1].options.map((gender) => {
    let currObject = {}
    currObject[nestedArray[0].id] = degree
    currObject[nestedArray[1].id] = gender
    arrayOfObjects.push(currObject)
})
})

console.log(arrayOfObjects)

but the problem with that is sometimes my nestedArray will consist of another nested array called age forexample How can i make this "univsersal" in a sense? Fiddle: https://jsfiddle.net/j2v4L918/15/

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

0

It appears you're looking for the cartesian product. Here is an example using one of the answers from this question: Cartesian product of multiple arrays in JavaScript.

/**
 * @see https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript
 */
const cartesian = (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())));

const nestedArray = [
  { id: "Degree", options: ["HighSchool", "Undergraduate", "Bachelor", "Master", "Doctor"] },
  { id: "gender", options: ["male", "female"] },
  { id: "subject", options: ["spanish", "geometry"] }
];

const arrayOfEntries = nestedArray.map(({ id, options }) => options.map(option => ({ [id]: option })));

const cartesianProduct = cartesian(...arrayOfEntries);

const arrayOfObjects = cartesianProduct.map(arr => Object.assign({}, ...arr))

console.log(arrayOfObjects);


Explanation

The snippet above begins by mapping your array of option objects to an array of individual objects using computed properties:

[
  { id: "gender", options: ["male", "female"] },
  { id: "subject", options: ["spanish", "geometry"] }
];
// mapped to individual objects: {[id]: option} 
[
  [ { gender: 'male' }, { gender: 'female' } ],
  [ { subject: 'spanish' }, { subject: 'geometry' } ]
]

It then takes the cartesian product of these entries:

// cartesian product of individual objects
[
  [ { gender: 'male' }, { subject: 'spanish' } ],
  [ { gender: 'male' }, { subject: 'geometry' } ],
  [ { gender: 'female' }, { subject: 'spanish' } ],
  [ { gender: 'female' }, { subject: 'geometry' } ]
]

Finally we map() over this array and merge the individual objects by passing them to Object.assign making sure to assign into an empty object, and using spread syntax (...) to expand the subarray of objects.

Object.assign({}, ...[{ gender: 'male' }, { subject: 'spanish' }])
//            ^^ assign into empty object to avoid reference problems
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!