-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
  • 639
  • 8
  • 18

2 Answers2

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

Have a look at source

Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50
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
  • 376,160
  • 25
  • 347
  • 392