How to use javascript array.filter

by Sai gowtham1min read
How to use javascript array.filter

In es6, we got more useful methods in javascript where the filter method help us to filter the particular set of elements from the arrays.

consider we have an array of numbers from 1 to 10, but we need only odd numbers from the array.

const numbers  = [1,2,3,4,5,6,7,8,9,10];

const oddNumbers = numbers.filter((e,i) => e % 2 === 1);


//output -> [1,3,5,7,9]

filter method accepts the callback function as a first argument , inside that callback function we need to specify our own condition.

We can also filter the objects by using the Array.prototype.filter method.

const users = [


Let’s write our own condition to filter present users from the above array.

const presentUsers = users.filter((user)=>user.present );


 /*  output

     [  { id: 1, name: "mas", present: true },
        { id: 3, name: "king", present: true },
        { id: 6, name: "seth", present: true }


Happy coding.