2

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/

Shivam
  • 3,514
  • 2
  • 13
  • 27
matieva
  • 365
  • 4
  • 12
  • If you were to have another object in the array, would you want to have all combinations of values for that as well. E.g. given another element `{id: "subject", options: ["maths", "german"]}` in the input array would you want all #combinations of degree, gender and subjects? – Mushroomator Apr 18 '22 at 20:08
  • @mushroomator Yes, that would than return{ Degree: "HighSchool", gender: "male", "subject": "maths" }, { Degree: "HighSchool", gender: "male", "subject": "german" }, { Degree: "HighSchool", gender: "female", "subject": "maths" } – matieva Apr 18 '22 at 20:13
  • Careful as `.map` intends to return an array. `.forEach` is a better use here – async await Apr 18 '22 at 20:15

1 Answers1

3

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
pilchard
  • 12,414
  • 5
  • 11
  • 23