snippetjavascriptMajor
Convert object array to hash map using lodash
Viewed 0 times
maplodashconvertarrayhashusingobject
Problem
The use case is to convert an array of objects into a hash map where one property is the key and the other property is the value. Common case of using this is converting a "link" object in a hypermedia response into a hash map of links.
jsfiddle
Is there a more elegant solution - i.e. both readable and efficient? I suspect that Lodash might have a function to do this already, but I haven't found anything like this after looking at the API documentation.
jsfiddle
function toHashMap(data, name, value) {
return _.zipObject(_.pluck(data, name),
_.pluck(data, value));
}
function toMap(data, name, value) {
return _.reduce(data, function(acc, item) {
acc[item[name]] = item[value];
return acc;
}, {});
}
var data = [
{ rel: 'link1', href: 'url1'},
{ rel: 'link2', href: 'url2'},
{ rel: 'link3', href: 'url3'},
{ rel: 'link4', href: 'url4'},
];
console.log(toHashMap(data, 'rel', 'href'));
console.log(toMap(data, 'rel', 'href'));toHashMap appears much more readable, but less efficient. toMap seems to have the best efficiency at the expense of readability.Is there a more elegant solution - i.e. both readable and efficient? I suspect that Lodash might have a function to do this already, but I haven't found anything like this after looking at the API documentation.
Solution
Revised Solution
As per Pau Fracés comment above, here is the complete solution. The solution given by John Anderson would index all objects by the key. However, this would not create a key-value pair map.
To complete the solution of generating a full hash map, the values must be mapped to the key. Using the mapValues function, the values can be extracted from the objects and mapped back to the key or in this case
Pseudo Code
Code
Below is the complete code with logging enabled. For a non-logging version, remove all lines with the
As per Pau Fracés comment above, here is the complete solution. The solution given by John Anderson would index all objects by the key. However, this would not create a key-value pair map.
To complete the solution of generating a full hash map, the values must be mapped to the key. Using the mapValues function, the values can be extracted from the objects and mapped back to the key or in this case
rel.Pseudo Code
- Index all objects by the chosen key.
- Map all values to the key.
Code
Below is the complete code with logging enabled. For a non-logging version, remove all lines with the
tap function.var data = [{ rel: 'link1', href: 'url1' },
{ rel: 'link2', href: 'url2' },
{ rel: 'link3', href: 'url3' },
{ rel: 'link4', href: 'url4' }];
function log(value) {
document.getElementById("output").innerHTML += JSON.stringify(value, null, 2) + "\n"
}
var hashmap = _.chain(data)
.keyBy('rel')
.tap(log) // Line used just for logging
.mapValues('href')
.tap(log)
.value();
Context
StackExchange Code Review Q#57614, answer score: 43
Revisions (0)
No revisions yet.