lodash: mapping array to object

前端 未结 8 2230

Is there a built-in lodash function to take this:

var params = [
    { name: \'foo\', input: \'bar\' },
    { name: \'baz\', input: \'zle\' }
];
8条回答
  •  既然无缘
    2020-12-23 14:50

    Another way with lodash 4.17.2

    _.chain(params)
        .keyBy('name')
        .mapValues('input')
        .value();
    

    or

    _.mapValues(_.keyBy(params, 'name'), 'input')
    

    or with _.reduce

    _.reduce(
        params,
        (acc, { name, input }) => ({ ...acc, [name]: input }),
        {}
    )
    

提交回复
热议问题