-2

I have this array

[ 0:'a', 1:'b', 2:'c']

I want convert this array to an object

{ 'a': true, 'b': true, 'c': true }

What is the best way ?

Hugues Gauthier
  • 571
  • 7
  • 15

2 Answers2

1
array.reduce((acc, value) => {
  acc[value] = true;
  return acc;
})

Have a look at source

Józef Podlecki
  • 9,034
  • 3
  • 20
  • 38
1

You could map entries from the array and get an object.

var array = ['a', 'b', 'c'],
    result = Object.fromEntries(array.map(k => [k, true]));

console.log(result);
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358