Let say I have an array:
const arr = ['a', 'b, 'c'];
I want to create an object like this:
{ 'a': true, 'b': true, 'c': true}
How can I do this?
const obj = {...arr: true}
did not work
Using Array#reduce
:
const arr = ['a', 'b', 'c'];
const res = arr.reduce((acc, key) => ({ ...acc, [key]: true }), {});
console.log(res);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With