how can I add several attributes to a sub-sub-object in ES6 by using the keys of objects from an array ?
I have this:
const data = [{
id: 'abc-test',
},
{
id: 'xyz-test',
},
];
const t = {
a: {
filters: {
tOne: [],
}
}
};
The result should look like this:
const t = {
a: {
filters: {
tOne: [],
'abc-test': [],
'xyz-test': [],
}
}
};
What I tried
...data.map((el) => [el.id]),
How to solve it ? I know that this could be done by using .forEach but I would like to use another ES6 Array.prototype functions if possible.
forEachis an "Array.prototype" function like any other.forEachis really the best choice here. The other functions have other usages. I mean you can usereducelikedata.reduce((acc, o) => (acc[o.id] = [], acc), t.a.filters)or usemapas aforEach, but that would be like using a shoe to spread butter on a piece of bread, you can do it, but why would you do that when you have a knife/forEach?!!