Convert array to object keys [duplicate]

What's the best way to convert an array, to an object with those array values as keys, empty strings serve as the values of the new object.

['a','b','c']

to:

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

Solution 1:

try with Array#Reduce

const arr = ['a','b','c'];
const res = arr.reduce((acc,curr)=> (acc[curr]='',acc),{});
console.log(res)

Solution 2:

You can use Array.prototype.reduce()and Computed property names

let arr = ['a','b','c'];
let obj = arr.reduce((ac,a) => ({...ac,[a]:''}),{});
console.log(obj);

Solution 3:

var target = {}; ['a','b','c'].forEach(key => target[key] = "");

Solution 4:

You can use Object.assign property to combine objects created with a map function, please take into account that if values of array elements are not unique the latter ones will overwrite previous ones

const array = Object.assign({},...["a","b","c"].map(key => ({[key]: ""})));
console.log(array);

Solution 5:

You can use array reduce function & pass an empty object in the accumulator. In this accumulator add key which is denoted by curr

let k = ['a', 'b', 'c']

let obj = k.reduce(function(acc, curr) {
  acc[curr] = '';
  return acc;
}, {});
console.log(obj)