Underscore.js _.filter Function

What is Underscore.js?

Underscore.js is the library in JavaScript which has a lot of functions like map, filter, chain, etc., and these functions are applied directly on the objects or arrays.

_.filter() function

The _.filter() function is used to check and filter those elements which satisfy some specific conditions. It stores them into a new variable. This function is applied to an array, and it creates a new array that contains only those elements which satisfy the condition. This function is generally used when the size of the given array is too large.

Syntax:

This function has two necessary and one optional parameter:

initialList: This is the list from which we filter the necessary elements.

condition: This is the condition that is checked for each element, and those elements which satisfy this condition are put into the resultant array.

[context]: This parameter contains some text which will be printed when we use this function, and it is an optional parameter.

Return value:

This function will return an array that will contain those elements which satisfy the condition.

Now, we will take some examples to understand the _.filter() function:

Example1:

JavaScript code:

Output:

Underscore.js _.filter Function

Explanation:

In the above code, we have first imported the library. Then we applied the _.filter() method on an integer array, and in the condition part, we put the function which returns the odd value. So, all the values of the array will pass into the function, and odd values will be stored in the new variable array. At last, we printed the resultant array, which only contains the odd values.

Example2:

JavaScript code:

Output:

Underscore.js _.filter Function

Explanation:

In the above code, we have first imported the library. Then we created a list of words, and we applied the _.filter() function on the list, and in the condition, we returned only those elements whose length is equal to 9. We got only one element in the resultant array, and we have printed it using the console.log() statement.

Example 3:

JavaScript code:

Output:

Underscore.js _.filter Function

Explanation:

In the above code, we have first imported the library. Then we have used a separate function which will be used as the condition for the _.filter() method. This function will return the number if the number is greater than 100. We applied the _.filter() method on an integer list, and in the condition part, we called the function. So we got the resultant array which contains those elements which are greater than 100.

Example 4:

JavaScript code:

Output:

Underscore.js _.filter Function

Explanation:

In the above code, we have first imported the library. Then we created a list of words to which we are going to apply the _.filter() method. Now we created a function and a parameter that returns the list if the parameter is the substring of any element.

In this function, we have applied the _.filter() method on the list, which takes each list element and converts it into a lowercase word using the toLowerCase() function. Then it also converts the given parameter of the function into a lowercase word. Then it used the indexOf function to find out the index of the parameter in the given word of the list. If the function returns value greater than and equal to zero, it means the parameter is the substring of the given word, and we will return the word into the resultant array.

In the above example, we will get those elements where the 'pt' word exists, and ' JavaScript' is the element that satisfies the condition.