I am working on an assignment. I have the following object form.
{
name: \"name here\",
skills: [ \"cash\", \"shares\" ],
subjects:
[
{
You could take a recursive function which separates all key/value pairs and build a new cartesian product by iterating the values, if an array with objects call getCartesian
again and build new objects.
The referenced link does not work, because of the given arrays of objects which the linked answer does not respect.
function getCartesian(object) {
return Object.entries(object).reduce((r, [k, v]) => {
var temp = [];
r.forEach(s =>
(Array.isArray(v) ? v : [v]).forEach(w =>
(w && typeof w === 'object' ? getCartesian(w) : [w]).forEach(x =>
temp.push(Object.assign({}, s, { [k]: x }))
)
)
);
return temp;
}, [{}]);
}
var data = { name: "name here", skills: ["cash", "shares"], subjects: [{ subName: "subject1", remark: ['remark1', 'remark2'] }, { subName: "subject2", remark: ['remark1', 'Hockey'] }] };
console.log(getCartesian(data));
.as-console-wrapper { max-height: 100% !important; top: 0; }