To get a specific element in a large array which satisfy the condition given using underscore js _.filter function

When we have an array which is bigger in size and some specific element is need to be fetched 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.

Syntax: _.filter(initialList,condition,[context]) 

Example code:

function greaterThanHundred(num) {  
    return num >= 100;  
}  
//applying the _.filter() method  
var res = [1, 4, 12, 134, 15, 69, 8, 330, 54].filter(greaterThanHundred);  
//printing the resultant array  
console.log(res);  

It will give a result of…134 and 330

It needs initialisation and a condition

Leave a comment

Your email address will not be published. Required fields are marked *