Say I have a JS Map like this:
[
{
id: 1,
name: "Foo",
contents: [1,2,3],
morecontents: ["a","b"],
},
{
id: 2,
name: "Bar",
contents: [4,5,6]
}
]
I want to be able to expand the arrays within it to produce a Map like this:
{
{
"id":1,
"name":"Foo",
"contents":1,
"morecontents":"a"
},
{
"id":1,
"name":"Foo",
"contents":2,
"morecontents":"b"
},
{
"id":1,
"name":"Foo",
"contents":3,
"morecontents":""
}
}
[...]
Note the arrays will always be at expected keys, and there may be multiple arrays to expand. In the event arrays are different lengths (very likely) the fields can be left blank.
How could this be achieved in Node.JS/vanilla JS?
Thank you.
CodePudding user response:
array.map(obj =>
obj.contents.map((content,i) =>
new Object({
...obj,
contents: content,
morecontents: obj.morecontents[i] ? obj.morecontents[i] : ""
})
).flat()
CodePudding user response:
Considering the arrays within the object are dynamic and anyone of these arrays could be of longer length a following solution will be more comprehensive. First it finds the arrays elements within the object, then finds the array with most elements, then it creates copies of object equal to the number of maximum lengths of its child array and lastly updates the elements according to the index of these copies. The results are then flattened into a single result.
array.map((obj, oi) => {
const arrayNames = Object.keys(obj).filter(k => Array.isArray(obj[k]));
let maxlen = arrayNames.map(a => obj[a].length).reduce((p, c) => p > c ? p : c);
const res = new Array(maxlen).fill({}).map(objn => Object.assign({}, objn, obj));
res.forEach((x, i) => arrayNames.forEach(name => x[name] = i < obj[name].length ? obj[name][i] : ""));
return res;
}).flat()
