For example; if we query a database we will most likely get the rows as an array of objects. Where each object represents a row in the database.
What is Higher Order Function?
What is Node JS?
How to use Filter, Map and Find method in Node js:
We will see one by one with examples:
1. How to use Filter method in Node Js
This filter method used to filter the array of elements with specific conditions, and reproduce the new array with the conditions
I have one array, it contains 7 names, and now I need an array with some conditions like the length of the name should be less than 4 characters. So if I am using the filter method here I can get the new array of names with the specified conditions, which is the length of a name less than 4.
The below image contains the example with the simple node application
2. How to use Map Method in Node JS :
The map method is used to change the values in a specific array or find the number of elements in an array with the specified conditions. It iterates the elements in the array and applies the condition on that, and returns only ‘true’ or ‘false’ in the new array.
The same above array I will take, now I applied the condition that I need to know how many names in that array is greater than 4 letters. So now it should give 4 names from that array. So the return array will have 4 true values and 3 false values.
The below image contains the above example
3. How to use Find Method in Node JS:
Find method used to find a specific or single element from the array. It gives only one value as a return. If we want we can make it true or false or we will get the value itself.
I take the above array and it contains a set of name lists, now I want to take the single name from that I can do. For that, we will use the find() method. Now I want “Deva” from that array so I can take it.
The below image contains the example with a simple node application
The constant emerging technologies in the world of web app development always keep the emerging technology trend evolving.