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/

3
  • 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? Commented Apr 18, 2022 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" } Commented Apr 18, 2022 at 20:13
  • Careful as .map intends to return an array. .forEach is a better use here Commented Apr 18, 2022 at 20:15

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.