Underscore.js: This is what I know

Today at the Iron Yard I learned about Underscore.js and want to share with you the functions that I think kick butt and are ready for action.

I will start with Collections:

_.find

Find is pretty sweet because it finds the 1st value that passes the truth test. This can be beneficial if you are needing to filter down an array to one value.

_.toArray

This function turns any array like list into an array. This can come in handy because once your list is in an array you are able to use array functions to filter it down if needed.

On to the Array functions!

_.flatten

This bad ace function takes an array that has arrays nested into it and flattens it out into a single array. This makes filtering an array much easier because you need less code if you want to reach into the nested arrays and pull out their values.

_.range

This function is great for creating a flexible list of integers. Oh the possibilities! I hear this comes in handy with each and map loops.

Time to get functional with Function functions.

_.bind

Bind is a pretty neat function. Basically when your function is called the value of this will be the object that is passed to the bind function.

_.delay

This function is similar to the setTimeout function. It will invoke a function after a set time. This can be helpful if you want to create some fun animations on your page like having the background color change after a set time. (Not sure if that would be a fun animation, but hey it’s late and I am seeing double at this point.)

Objects….

_.key

This guy when called returns the names of an objects enumerable properties.

_.values

I like this one because it will pull all of the values out of an object and return it into an array. This makes it easier to look through the list of values and pull out what you need when you need it.

Finally we have the functions from the Utility section.

_.iteratee

This little fella generates callbacks that then can be applied to each element of a collection to get the desired result. That could be pulling out a specific key’s values from an object as shown below.

_.unescape

This function replaces things such as & etc with their unescaped counterparts such as &. I assume this would be helpful when testing and viewing things in the browser console.

That is all I got folks. If you made it this far I will reward you with a funny gif.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.