Examples of the _.keyby method in lodash

If I am every in a situation in which I need to create an object with keys that are generated from the properties of objects in an array I can use the lodash _.keyBy method to make quick work of that. The _.keyBy method works a lot like _.groupBy only it will only create one object for each key. In this post I will be going over some use case examples of _.keyBy, and some vanilla js alternatives for doing so as well.

1 - What to know

This is a post on the _.keyBy method in lodash, which can be used to create a new object with keys that are made from the properties of objects in an array. This is not a getting started post on lodash, or javaScript in general. Lodash has a lot of useful methods that can help save time when it comes to working these thinks out from scratch, but a lot of the methods are now part of javaScript itself, and much of what is left is often not to hard to write from scratch. I have not attachment to lodash, and in these posts on lodash I often write about how to do something with lodash, and then also how hard it is to get by without it.

2 - basic example of _.keyBy

For a basic example I just have an array of objects that each have a type and price property. I want to have a single object that has keys that are the type of an object in the array followed by a number. The _.keyBy method can be used in this case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
let units = [{
type: 'a',
price: 10
}, {
type: 'b',
price: 12
}, {
type: 'a',
price: 9
}
];
let counts = {
a: 0,
b: 0
};
let keyed = _.keyBy(units, function (unit) {
counts[unit.type] += 1;
return unit.type + counts[unit.type];
});
console.log(keyed.a1.price); // 10

2.1 - basic example with vanilla js

It is not to hard to make a vanilla js alternative to this.

1
2
3
4
5
6
7
8
9
10
11
let counts = {
a: 0,
b: 0
},
keyed = {};
units.forEach((unit) => {
let c = counts[unit.type]++;
keyed[unit.type + c] = unit;
});
console.log(keyed.a1.price); // 9

3 - Conclusion

So _.keyBy is one of the many methods in lo dash that can come in handle now and then when a situation comes about that calls for it’s use. If you have anything you might like to add, or have any questions or concerns about the content of this post please be sure to let me know in the comments, and thank you for reading.