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