By reading this post, you will become a master of this 5 awesome array methods which will make your life more easier.
Method - 1: map()
This method is a kind of loop. You can loop through an array by using this method. It takes 3 parameters the first one is returning us the data/ value, the second one is the index number and the third parameter returns us the whole array.
const ara = [ 'Apple', 'Banana', 'Kiwi' ];
ara.map(( data, index, theArray ) => {
console.log(data, index, theArray);
});
Method - 2: forEach()
This method is very similar to the method no -1. So this is also a kind of loop you can loop through an array with this method.
const ara = [ 'Apple', 'Banana', 'Kiwi' ];
ara.forEach(( data, index, theArray ) => {
console.log(data, index, theArray);
});
Difference between map() and forEach()
forEach() -> This iterates over a list and applies some operation with side effects to each list member (example: saving every list item to the database)
map() -> This iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (example: transforming list of strings to uppercase). It does not mutate the array on which it is called (although if passed a callback function, it may do so).
Method -3: filter()
Well this method is my most favourite one. What is does, is it can filter through an array. Suppose you have an array. In this array you have put all the prices of something. Now you want the prices from the array which are only under 100. How can we do that? So in this case, we can use the filter() method. Let me show you an example.
const prices = [ 100, 200, 50, 30, 400, 50, 30, 450, 89, 90 ];
const under100 = prices.filter(( value, index, array ) => {
return value < 100;
});
console.log(under100);
Now the prices under 100 will be stored under a separate array and then you can use it by just calling the variable ``under100``. This method takes three parameters. As same as the other methods. The first one is The value, second one is the unique index number and the third one returns the whole array.
Method - 4: includes()
This method is also a very useful one. Suppose you have an array and under that, you have put some names of people now you want to know that is the name - "Jack" is available under that array! So in this case you can use this awesome method.
const names = [ 'July', 'Jack', 'Lauv"];
const isItHere = names.includes('Jack');
console.log(isItHere);
This method will return true or false/ Boolean value if it is containing the value under the array
Method - 5: reduce()
This method is really really helpful. Suppose you have an array of numbers and Now you want to get the sum of the numbers in the array. So in this case, you can use this method.
const numbers = [ 12, 23, 34, 45, 5, 7 ];
const subtract = numbers.reduce(( value, index, array ) => {
return value + index;
});
consolelog( subtract );
You can also concat strings with this array method. It takes the same parameters as like the others. First one the value, second one is the index number and the third number is the whole array.
So these are the top 5 javascript array methods. Thanks for reading this article. If you found any wrong informations here please let me know. And make sure you follow me to recieve all the informational posts just like that.
:)
Top comments (0)