Every now and then I like to play around with one of the methods in lodash such as the _.countBy collection method that I will be writing about in this post. The lodash countby method can be used to create an object where each key is the result that is return by a method that is called for each element in a collection. Each value is the count for that key value that is returned when calling the method used with count by for each collection element.
In other words the _.countBy method returns a new object with keys, and values generated from a given method with a source collection object. The return value of the method given to the count by method can be any string value that will work well as a key name for an object property, and there will be a count for each key.
The source code examples in this post can be found in my test lodash github repository.
Say I have an array of primitive values such as numbers, and I want to have an object that is a count of each primitive value in this array of numbers. One way of going about doing this would be to use the lodash identity method with the lodash count by method. This can be done by calling the lodash count by methid, passin gthe array of numbers as the first argument, and then passing the identity method as the second argument, or better yet just passing the array only as the first argument sense the identity method just happens to be the default methods that is used with the count by method.
For a basic example of _.countBy I just made an array of objects, that contain some data for a collection of people. each object has a username and score property that contains a number value between 0 and 100. What I want to do is create an object that contains two keys pass and fail where pass is a count of objects of people who have a score above 65, and fail is the remaining count of objects. So for this I can use the lodash countBy method to make an object of pass, and fail properties that is a count of how many objects meet the condition that I defined in the method that I gave it, along with the array of objects.
One feature of the count by method is that a lodash property method short hand can be used. The property method of lodash is one method in lodash that will return a function that can be passed to a function like that of the lodash count by method.
In this section I will now be going over a few quick use case examples, now that I have the basics of the method out of the way. This is a section that I might get around to expanding now and then each time I come around to doing a little editing, because once every little detail is covered when it comes to the methods itself this is the only thing left to write about for the most part.
If I want to get a count for each and every word in a text source, rather than just over all word count, then the count by method could be used for this. When it comes to creating an array from a string one quick way to go about doing it would be to just one the array split method and give a space as the first and only argument. Although that might work okay for this example, when it comes to a real text source a bot more would need to be done when it comes to creating what is often called a tokenizer.
Now for a quick example of using the lodash count by method to create a score delta value. This will not be a full working game example mind you, just a little code that has to do with adding up what a score should be for an array of dead enemy units. So with that said say I have an object that defines stats for all the unit types in a game. When it comes to having an object pool of enemy units each of those objects should have a type property that can be used to reference this units object. So then the count by lodash method could be used as a way to get a count for each of the units in a dead unit collection, and then it would just be a matter of multiplying a points worth value for each count to add up a delta to add to the score value.
Although this would work fine I have to admit that when it comes to working on a real game project I would likely just directly loop over the dead units collection and add up the point values that way. However I just wanted to add a quick use case examples like this as a way to just have a use case example that has something to do with games.
There is not direct native counterpart for the lodash count by method unlike many other methods in lodash. However this is something that is still not all that hard to do with just a little native javaSccript. So in this section I will be going over a few source code examples that produce the same end result as the lodash count by method without using lodash.
In this example I am making a count by method that will work with arrays and objects in general by making use of the object keys static method. This Object keys method will create and return an array of public key names for the object that I give it. I can then loop over the array of key names to have a key name as well as a value for each element in the object collection, as this should work fine for both arrays and objects in general. That is that in the event that I pass an array to the object keys method that will give me an array of numbered keys while other objects will given me whatever the key names are.
So then when looping over the array of pubic key names for a source object, I can call a given function and pass the value, key, and the object itself. The return value of the call of this given function can then be used as a key name for a count object created in the body of the count by method. So then I can use the returned key name to test if the value for the count object is undefined. In the event that the key is undefined I can create the key and set the value to 1, else step what it is that should be a number by one.
On top of the count by lodash method there is also a key by and group by methods in lodash also. In this section the source code examples will be about these other lodash methods, and as such what they share in common with the lodash count by method. Also of course the examples should also show how the key by and group by methods differ from the count by method.
The key by method works more or less the same way as the count by method in the sense that the return value of the function given to the method will be the key value for the new object that is returned. However this time it is not abut racking up a count but just assigning a single value to a key in the object. When dealing with a collection of object like the one covered in the basic section of the post this might not result in a desired result.
The lodash keyby method might work okay when it comes to recreating what the public keys are for an object, but it is not a good choice for getting a count of something, or creating groups. However there is a method in lodash that might prove to be a more versatile choice from count by called the lodash group by method.
The lodash count by method will create an return a new object where head key is a return value of a given function, and the value will be a primitive that is the count of times that key value has happened. However if I want to have an array of values for which that has happened rather than a count there is the group by method.
So now for a quick recap of all the choices when it comes to key by, group by, and count by.
So then there is what is in common where each of the methods create an new object with keys that are return values of a given function. The difference between them has to do with what the values for these keys should be. The key by method will just simply do that, and the value will be whatever the last value was when going over the collection. The group by object will do the same only it will create arrays of values for these key values of the object, and the count by method will give a number primitive for each key.
This _.countBy method might come in handy for some situations in which I quickly want to make an object that has keys, and values that are the result of some kind of condition. This makes _.countBy one of the methods in lodash to be aware of to make quick work of any kind of situation where it would come in handy.