We know find method looks for a single (first) element. But filter will check all passed data and return an array.
The syntax is same as find method,
let results = arr.filter(function(item, index, array) {
..................
});
Example:
From the above we are passing an item and checking the condition which specifies id is less than 3. So passed data as returned as array.
The syntax is same as find method,
let results = arr.filter(function(item, index, array) {
..................
});
Example:
let usersArrOfObject = [ {id: 1, name: "John"}, {id: 2, name: "Pete"}, {id: 3, name: "Mary"}, {id: 4, name: "Maths"}, {id: 5, name: "Mercy"} ]; let outputUsers = usersArrOfObject.filter(item => item.id < 3); alert(outputUsers.length); // 2 console.log(outputUsers);
From the above we are passing an item and checking the condition which specifies id is less than 3. So passed data as returned as array.
No comments:
Post a Comment