Hello everyone hope you are all doing well, My name is Surya L.
The aim of this blog is to teach you find( ) and filter( ) Method in JavaScript.
-
The find( ) method is used to locate all the descendant elements of a selected element.
-
Only the child elements are searched with the find() method.
It does not execute the function for empty elements.
- Its syntax is -:
array.find(function(value, Index, array),thisValue)
-
This method returns undefined if no elements are found.
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12
-
filter( ) Method returns the elements that match and removes those that do not match.
-
The filter() method is used to filter all the elements.
-
Its syntax is -:
array.filter(function(value, Index, array), thisValue)
In filter() method a value is passed to the function as its this value.
const array1 = [5, 12, 8, 130, 44];
const found = array1.filter(element => element > 10);
console.log(found);
// output: [12, 130, 44]
- .find( ) Method returns the first element which satisfies the condition inside .find( ) Method.
const array1 = ["Apple", "Ball", "Cat", "Doll", "Elephant","Fish"];
const found = array1.find(element => element.length > 4);
console.log(found);
//Output : Apple
- .filter( ) method returns all element whose string length is great then 4
const array1 = ["Apple", "Ball", "Cat", "Doll", "Elephant","Fish"];
const found = array1.filter(element => element.length > 4);
console.log(found);
//Output : ["Apple", "Elephant"]
-
.find( ) Method returns the first element which satisfies the condition inside .
-
.filter( ) method returns all element which satisfies the condition.
Thanks for reading the blog. Do let me know what you think about it.
You can connect me with Showwcase Twitter Blog GitHub Contact Me