I have an array of objects with keys and values as follows:
let input = [
{ "b1": [ 1, 0 ] },
{ "b2": [ 1, 6 ] },
{ "total": [ 0, 4 ] },
{ "b3plus": [ 0, 2 ] }
]
I want to rename the keys of this array of objects so that the final output looks like this:
let output = [
{ "B1": [ 1, 0 ] },
{ "B2": [ 1, 6 ] },
{ "Total": [ 0, 4 ] },
{ "B3+": [ 0, 2 ] }
]
The code I tried is as follows:
const output = input.map(({
b1: B1,
b2: B2,
b3plus: B3plus,
total: Total,
...rest
}) => ({
B1,
B2,
B3plus,
Total,
...rest
}));
However, this code is not successful because renaming B3+
causes an error during compilation. Even if I skip renaming b3plus
, the output is not as expected because undefined objects are added. Can someone help me identify where I am making a mistake?
Here is the error https://i.sstatic.net/nbGG97PN.png
If I do not rename b3Plus
and proceed with renaming the other keys, I see unnecessary undefined objects. How can I avoid including those undefined objects?