The lodash keys method in lodash can be used to get an array of public key names of an object. There is also the native Object.keys method as well that has been introduced in recent years. In addition a for in loop is another option for getting object keys that has great backward compatibility with older environments, so this makes the lodash _.keys method one of many methods in lodash that make me scratch my head wondering if I should bother or not.
1 - lodash keys and Object.keys example
The lodash keys method works by just simply passing an object as the first argument, and an array of public key names is returned by the method. The native Object.keys method works in the same manner as well but it might not always be there when it comes to older browsers.
So because there is a native method for getting public key names of an Object, what bother with lodash? Well first off this is just one method, and there are many lodash methods where there is no native counter part at all. Also there are many lodash methods where the lodash method works a little differently, or brings a little something more to the table.
The lodash keys method might not be the best example of the worth of lodash these days, but there is something to say about browser support with the Object.keys method. The native Object.keys method is still fairly new in light of the history of web development, and it is also true that the method is not supported at all when it comes to Internet Explorer. So it still makes sense to use the lodash keys method as a way to bring better browser support. If you do not want to use lodash, it is still wise to use a polyfill for this one.
2 - A for in loop as a lodash keys replacement, and Object.keys pollyfill
It is not to hard to write a keys method using a for in loop. If backward compatibility is of concern using for in might be the best option, however if performance is a concern you might wish to work something else out. Still both a stand alone keys method can be added super easy, and it can also be used as a polyfill in the event that Object.keys is not there.
There is also a _.forIn lodash method that can also be used to create an array of public or own property names of an Object as well.
3 - Conclusion