lodash: mapping array to object

JavascriptLodash

Javascript Problem Overview


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

var params = [
	{ name: 'foo', input: 'bar' },
	{ name: 'baz', input: 'zle' }
];

And output this:

var output = {
	foo: 'bar',
	baz: 'zle'
};

Right now I'm just using Array.prototype.reduce():

function toHash(array, keyName, valueName) {
	return array.reduce(function(dictionary, next) {
		dictionary[next[keyName]] = next[valueName];
		return dictionary;
	}, {});
}

toHash(params, 'name', 'input');

Wondering if there's a lodash short-cut.

Javascript Solutions


Solution 1 - Javascript

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 }),
    {}
)

Solution 2 - Javascript

You should be using _.keyBy to easily convert an array to an object.

Docs here

Example usage below:

var params = [
    { name: 'foo', input: 'bar' },
    { name: 'baz', input: 'zle' }
];
console.log(_.keyBy(params, 'name'));

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

If required, you can manipulate the array before using _.keyBy or the object after using _.keyBy to get the exact desired result.

Solution 3 - Javascript

Yep it is here, using _.reduce

var params = [
    { name: 'foo', input: 'bar' },
    { name: 'baz', input: 'zle' }
];
 
_.reduce(params , function(obj,param) {
 obj[param.name] = param.input
 return obj;
}, {});

Solution 4 - Javascript

This seems like a job for Object.assign:

const output = Object.assign({}, ...params.map(p => ({[p.name]: p.input})));

Edited to wrap as a function similar to OP's, this would be:

const toHash = (array, keyName, valueName) => 
    Object.assign({}, ...array.map(o => ({[o[keyName]]: o[valueName]})));

(Thanks to Ben Steward, good thinking...)

Solution 5 - Javascript

This is probably more verbose than you want, but you're asking for a slightly complex operation so actual code might be involved (the horror).

My recommendation, with zipObject that's pretty logical:

_.zipObject(_.map(params, 'name'), _.map(params, 'input'));

Another option, more hacky, using fromPairs:

_.fromPairs(_.map(params, function(val) { return [val['name'], val['input']));

The anonymous function shows the hackiness -- I don't believe JS guarantees order of elements in object iteration, so callling .values() won't do.

Solution 6 - Javascript

You can use one liner javascript with array reduce method and ES6 destructuring to convert array of key value pairs to object.

arr.reduce((map, { name, input }) => ({ ...map, [name]: input }), {});

Solution 7 - Javascript

Another way with lodash

creating pairs, and then either construct a object or ES6 Map easily

_(params).map(v=>[v.name, v.input]).fromPairs().value()

or

_.fromPairs(params.map(v=>[v.name, v.input]))

Here is a working example

var params = [
    { name: 'foo', input: 'bar' },
    { name: 'baz', input: 'zle' }
];

var obj = _(params).map(v=>[v.name, v.input]).fromPairs().value();

console.log(obj);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Solution 8 - Javascript

It can also solve without using any lodash function like this:

let paramsVal = [
    { name: 'foo', input: 'bar' },
    { name: 'baz', input: 'zle' }
];
let output = {};

paramsVal.forEach(({name, input})=>{
  output[name] = input;
})


console.log(output);

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncoreView Question on Stackoverflow
Solution 1 - JavascriptstasovlasView Answer on Stackoverflow
Solution 2 - Javascriptdanday74View Answer on Stackoverflow
Solution 3 - JavascriptJagdish IdhateView Answer on Stackoverflow
Solution 4 - JavascriptRainedColonyView Answer on Stackoverflow
Solution 5 - JavascriptdjechlinView Answer on Stackoverflow
Solution 6 - JavascriptFaisal HasnainView Answer on Stackoverflow
Solution 7 - JavascriptKoushik ChatterjeeView Answer on Stackoverflow
Solution 8 - JavascriptZeeshan Afzal SattiView Answer on Stackoverflow