Lodash: make hash like object from array

Is there an easy way in lodash to convert an array of type ['a', 'b', 'c']

to object of type { a: true, b: true, c: true }

? Or _.mapValues(_.invert(array), _.constant(true))

the easiest way to do it?

+3


source to share


1 answer


This works with the newest version of lodash:

var myArr = ['a', 'b', 'c'];
_.zipObject(myArr, _.fill(new Array(myArr.length), true));

      

Please note that you cannot do:



_.zipObject(myArr, _.fill(myArr, true));

      

Since fill () mutates the array passed to it.

+2


source







All Articles