If you want to learn React, it's important to get a fair understanding of some core JavaScript concepts first. Show
So if that's what you're doing, first of all – great job! You have made a wise decision by not starting directly with React. Second, React builds on key concepts like the map(), filter(), and reduce() JavaScript methods (after all – React is a JavaScript library). So this makes these methods a must-learn. Map, filter, and reduce are three of the most useful and powerful high-order array methods. In this tutorial, you'll see how each of these high-order array methods work. You'll also learn where you'll want to use them and how to use them, with the help of analogies and examples. It’ll be fun! How to Use the map() MethodSuppose you have an array 0 where you’ve stored some numbers, and you’d like to perform some calculations on each of them. But you also don’t want to mess with the original array.This is where 2 method will help you do this:
map() takes a maximum of three arguments, which are value/element, index, and array.
Let’s say you want to multiply each element by 5 while not changing the original array. Here's the code to do that:
And here's the output:
So what's going on here? Well, I have an 0 array with 6 elements in it. Next, I initialized an arrow function 4 with ‘num’ as an argument. This returns the product of 5 and 5, where the ‘num’ variable is fed the data by the map() method.If you’re new to arrow functions but are familiar with regular functions, an arrow function is the same as this:
Then, I initialized another variable 6 that will store the new array that the map() method will create.On the right-hand side, I called the map() method on the ‘arrOne’ array. So, the map() method will pick each value of that array starting from the index[0] and perform the desired calculation on each value. Then it'll form a new array with the calculated values. Important: Notice how I’m stressing not changing the original array. That is because this property is what makes the map() method different from the ‘forEach()’ method. The map() method makes a new array whereas the ‘forEach()’ method mutates/changes the original array with the calculated array. How to Use the arrOne.map(value/element, index, array)7 MethodThe name kind of gives it away, doesn't it? You use this method to filter the array based on conditions you provide. The filter() method also creates a new array. Let’s take an example: Suppose you have an array 8 and that array stores a bunch of numbers. Now, you would like to see what numbers can be divided by 3 and make a separate array from them.Here's the code to do that:
And here's the output:
Let's break down this code. Here, I have an array 9 with 7 elements in it. Next, I initialized a function 0 with ‘num’ as an argument. It returns true or false for each time a new num is passed for the comparison, where the ‘num’ variable is fed the data by the filter() method.Then, I initialized another variable 1 that will store the new array that the filter() method will create.On the right-hand side, I called the filter() method on the 9 array. So, the filter() method will pick each value of that array starting from the index[0] and perform the operation on each value. Then it'll form a new array with the calculated values.How to Use the reduce() MethodLet’s say you are asked to find the sum of all elements of an array. Now, you could use a for loop or the forEach() method, but reduce is built for this kind of task. The 3 method reduces an array to a single value by performing the desired operation on the elements collectively.Let’s take the above example and use reduce on it:
Here's the output: 4Everything is the same as the map() and filter() methods – but what’s important to understand is how the reduce method works under the hood. There’s not a definite of the reduce() method. Let’s see the simplest one and that will give you the gist of all the ways you can use reduce(). Here's some example syntax for 3:
Take a look at this syntax. Here, reduce is taking two arguments, 6 and 7, where 6 acts as an accumulator while the 7 has the index value.Now, on the first run the accumulator is equal to zero and 7 holds the first element of the array. What reduce does is that it throws the value in the accumulator that a2 holds and increments it to the next one. After that, the reduce() method performs the operation on both operands. In this case it is addition.So, basically 6 is the accumulator which is currently zero and 7 holds 15. After the first run, the accumulator has 15 in it and 7 is holding the next value which is 39.So, 4Now, on second run, reduce throws 7’s value, 39 in the accumulator and then performs the operation on both operands.So, 6Now, on the third run, the accumulator has a sum of 15 and 39 which is 54. 7 now holds 20 which the reduce method throws at the accumulator which sums up to 8.This process keeps on going until the end of the array. Signing OffWell, that’s it, everyone! Hope you have a good idea of how these high-order array methods work now. Consider sharing if you had a good time reading it and find it helpful. Check out my latest story here, and for my Git eBook, check here. ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT 20, Writer, Curator @FreemiumStuff, Developer If you read this far, tweet to the author to show them you care. Tweet a thanks Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started Apa itu Reduce di JavaScript?reduce() merupakan method array di JavaScript yang mengeksekusi fungsi callback pada setiap elemen array, nilai hasil kalkulasi pada elemen sebelumnya digunakan untuk melakukan kalkulasi pada elemen berikutnya. Setelah menjalankan fungsi callback untuk semua elemen array, method ini menghasilkan nilai tunggal.
Method apa yang dapat kita gunakan untuk memilah elemen array berdasarkan kondisi tertentu dan akan membuat sebuah array baru?filter()
Metode ini berfungsi untuk membuat sebuah array baru dengan memperhatikan kondisi tertentu pada setiap elemen dari array yang sudah ada.
Apa itu Map di JavaScript?. map() dapat digunakan untuk melakukan iterasi objek dalam suatu array dan, dengan cara yang serupa seperti array tradisional, memodifikasi konten dari setiap objek individu dan memberikan array yang baru. Modifikasi ini dilakukan berdasarkan apa yang dikembalikan dalam fungsi callback.
|