Given the array:
var arr = [ "one", "two", "three" ];
Whats the cleanest way to convert it to:
{ "one": true, "two": true, "three": true }
I tried the following but I imagine there is a better way.
_.zipObject(arr || {}, _.fill([], true, 0, arr.length))
var obj = arr.reduce(function(o, v) { return o[v] = true, o; }, {});
Using lodash:
const arr = ['one', 'two', 'three'];
_.mapValues(_.keyBy(arr), () => true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With