Merge nested arrays into unique objects

Viewed 210

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/

1 Answers

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
Related