76

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: ''
}
Miguel Stevens
  • 7,579
  • 15
  • 60
  • 112
  • 2
    Can you precise what you mean by "best way"? the more efficient will be a loop, the shortest text will be a functional approach (and all answers will be functional because it's trendy) – Kaddath Feb 20 '19 at 15:08
  • 7
    `Object.fromEntries(['a', 'b', 'c'].map(k => [k, '']));` – luukvhoudt Nov 14 '21 at 20:15

5 Answers5

142

try with Array#Reduce

const arr = ['a','b','c'];
const res = arr.reduce((acc,curr)=> (acc[curr]='',acc),{});
console.log(res)
prasanth
  • 21,342
  • 4
  • 27
  • 50
  • 4
    whatt does returning `(a[b] = '',a)` mean? what does the second argument do? – Shivam Sahil Dec 25 '20 at 19:18
  • 3
    @ShivamSahil check now, I have edited the answer. Each time of the loop `current value` inserted into accumulator `acc[curr] = ''` like this. `,acc` is returning the accumulator once loop end. And `,{}` is defined the accumulator is a object – prasanth Dec 26 '20 at 07:14
  • 3
    Could somebody add a Typescript version of this? – cbdeveloper Feb 18 '21 at 16:51
  • 2
    @cbdeveloper typescript version https://www.mycompiler.io/view/AlipcFS – prasanth May 28 '21 at 07:53
  • 3
    @ShivamSahil a good explanation of how the comma operator works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator – Cezar D. Oct 22 '21 at 19:55
39

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);
Maheer Ali
  • 34,163
  • 5
  • 36
  • 62
14
var target = {}; ['a','b','c'].forEach(key => target[key] = "");
Alexus
  • 1,035
  • 10
  • 16
  • 1
    Why do you use `map` to iterate over an array ? You could have chosen `forEach` – Serge K. Feb 20 '19 at 15:10
  • forEach would work as well. – Alexus Feb 20 '19 at 15:26
  • 4
    Usually [Array.prototype.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) is used to create a new array and the function passed to map is a pure function. – HMR Feb 20 '19 at 15:32
  • 7
    I don't understand why everybody was trying to complicate everything with reduce when there was such an obvious and simple answer – Tofandel Jun 13 '20 at 23:46
  • 5
    @Tofandel because this is perfect and handy usage of `reduce`... `forEach` requires variable declaration, `reduce` is true one-liner and this is kind of task it was designed for. It's definitely not 'complicated'. – user0103 Jan 28 '21 at 21:27
  • 3
    "True one-liners" are just making code more obscure and harder to understand. Better to add one line and have a code you can understand right away – Tofandel Jan 29 '21 at 10:13
10

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);
Krzysztof Krzeszewski
  • 5,299
  • 2
  • 14
  • 27
7

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)
brk
  • 46,805
  • 5
  • 49
  • 71