Assuming we have the following array structure of n
length:
const sourceArray = [
{ "dynamicKey1": true },
{ "dynamicKey2": false },
{ "dynamicKey3": true },
...
]
How could I tersely reduce this array to a single object of dynamic length:
const convertedObj = {
"dynamicKey1": true,
"dynamicKey2": false,
"dynamicKey3": true
...
}
Thanks!
3
Answers
Use reduce and
...
to spread the object in array.You can achieve this using
Array.prototype.reduce()
andObject.assign()
to mergecurr
intoacc
(starts of as{}
initially), copying the key-value pairs.: