Is there a more straightforward way in lodash to achieve the following,
var o = _.reduce([2, 3, 7], function(acc, v, i) {
acc[v] = i || "0";
return acc;
}, {});
Result,
Object {2: "0", 3: 1, 7: 2}
Try to use .reduce()
with a starting value of an empty object
,
var obj = [2, 3, 7].reduce(function(a,b,i){
return (a[b] = i, a);
}, {});
If you want it to be shorter even more then use E6 version,
var obj = [2, 3, 7].reduce((a,b,i) => (a[b] = i, a), {});
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