Underscore.js - Map Array of key/value pairs to an Object - One liner

后端 未结 8 927
囚心锁ツ
囚心锁ツ 2021-02-03 17:53

I\'ve been going through the underscore docs but I can\'t seem to find a method (or nested method call) to do the following transformation:

Let\'s say I have the followi

8条回答
  •  無奈伤痛
    2021-02-03 18:35

    This should do it:

    _.reduce(array, function(o, v){
        o[v.name] = v.value;
        return o;
    }, {});
    

    As a one-liner (you are kidding me, right?):

    _.reduce(array,function(a,b){a[b.name]=b.value;return a},{});
    

提交回复
热议问题