Today we are going to tell you about 6 most useful methods of the array in Javascript. These methods can help you perform various operations with an array very easily. So without further ado lets discuss these methods.
Filter
This method tops my list and as the name suggests it helps you filter out the array and get a new array with only those elements that pass the given condition.
Filter() method accepts a callback which is executed against every element in the array. If that callback results in a true value, that particular element is added to a new array and finally that new array is returned.
Array.prototype.filter(callback(item));
Example:
In the below code block we are trying to filter out
(i). the users which belong to country India
(ii). the user whose age is greater than 18
Map
If you want to update each element of an array then map() is what you are looking for.
The map() method is used to apply a function on every element in an array and then return a new array. This method does not mutate the original array instead returns a new array of updated values.
Array.prototype.map(callback(item));
Example:
In the below code block we are trying to get a new array with only user names and not user objects.
Reduce
If you are looking for something which takes an array and gives you a single output value out of the whole array then your search ends here.
The reduce() method is used to apply a function to each element in the array to reduce the array to a single value.
arr.reduce(callback, initValue);
callback – the function to execute on each element in the array.
initialValue – optionally supplied initial value. If no initialValue is supplied, the first element in the array will be used and skipped.
Example:
In the below code block we are trying to find out
(i). sum of all the numbers in array1
(ii). sum of all the numbers in array1 when inital value=5
Find
Unlike Array.filter() which gives you an array of elements that pass the condition, Array.find() gives you the very 1st element that passes the condition.
So if you only need a single value, use find()! When you need to find/return multiple values, reach for the filter() instead.
Array.prototype.filter(callback(item));
Example:
In the below code block we are trying to find out
(i). the 1st user who belongs to country India
(ii). the 1st user whose age is greater than 18
Some
The some() method tests whether at least one element in the array passes the testing condition implemented by the callback function. It only returns false if none of the array elements match the testing condition.
Array.prototype.filter(callback(item));
Example:
In the below code block we are trying to find if there is
(i). at least one user who belongs to country India
(ii). at least a user whose age is greater than 18
(iii). at least one user who belongs to country UAE
Every
The every() method tests whether all the elements in the array pass the testing condition implemented by the callback function. It only returns true if all of the array elements match the testing condition.
Array.prototype.filter(callback(item));
Example:
In the below code block we are trying to find whether
(i). all the users belonging to country India
(ii). all the users older than 18 years of age.
(iii). all the users are below 60 years of age.
SEO Company
Awesome post! Keep up the great work! š
AffiliateLabz
Great content! Super high-quality! Keep it up! š