I have this bit of code:
const calcRowCssClasses = (<string[]>context.dataItem.cssClasses).map(
(cssClass) => {
return { [cssClass]: true };
}
);
This produces the following array of objects:
{ "custom-calcrow-1": true },
{ "custom-calcrow-2": true }
Now, I have a starting object like this:
{ "calcrow": true}
The questin is, how can I add the above two entries to the existing object, such that the output will be an object like this:
{
"calcrow": true,
"custom-calcrow-1": true,
"custom-calcrow-2": true
}
Basically, I would like to spread an array of objects. Doing like this
return { calcrow: true, ...calcRowCssClasses };
has this result:
0: {custom-calcrow-1: true},
1: {custom-calcrow-2: true},
calcrow: true
Which isn’t the expected result.
Thanks.
2
Answers
Instead of .map() use Array.prototype.reduce
You could take
Object.assign
with spreading the array as parameters.