Array
JS object. If you are still not using map()
and reduce()
, now is the time to start. Most modern JS platforms natively support ECMAScript 5. Using these methods will make your code much cleaner, more readable and easier to maintain. map()
and reduce()
will help you take the path of more elegant functional design.map()
and reduce()
will make it possible to derive more benefits from the improvements of the JS engine, as browsers are optimized for their use. If you do not have performance problems, it is better to write code with optimistic calculation for the future. And the techniques to improve performance, making the code less tidy, leave for later, when they need arises.for
loop. For example: var animals = ["cat","dog","fish"]; var lengths = []; var item; var count; var loops = animals.length; for (count = 0; count < loops; count++){ item = animals[count]; lengths.push(item.length); } console.log(lengths); //[3, 3, 4]
animals
contains the source words,lengths
array will contain the results of the operation,item
variable is used to temporarily store each of the array elements that we manipulate during the execution of each cycle,for
array contains the internal variable count
and is optimized using the loops variable.animals
: we calculate the length and put it in the lengths
array.animals[count]
directly to the array lengths
. The code would be a bit shorter, but less readable even in this simple example. Similarly, to slightly improve performance, one could use the known length of the animals
array to initialize the lengths
array as new Array(animals.length)
, and then, instead of using push
add elements by index. But that would also make the code a little less understandable. In general, it all depends on how you will use your code in real projects.map()
, then the classic way will immediately seem too cumbersome.map()
: var animals = ["cat","dog","fish"]; var lengths = animals.map(function(animal) { return animal.length; }); console.log(lengths); //[3, 3, 4]
animals
. But besides it, we only declare lengths
, and directly assign to it the result obtained by mapping an anonymous embedded function into each element of the array of animals
. An anonymous function performs an operation on each animal and returns the length. Eventually, the array lengths
, containing the lengths of each word, becomes the same length as the original animals
.getLength()
and use it as follows: var animals = ["cat","dog","fish"]; function getLength(word) { return word.length; } console.log(animals.map(getLength)); //[3, 3, 4]
map
function to operate on each element.map
function returns a functor of the same size.map()
, except that instead of creating another functor, reduce()
produces a single result that can be of any type. For example, you need to get as a number the sum of the lengths of all the words in the array of animals
. You will probably immediately write something like this: var animals = ["cat","dog","fish"]; var total = 0; var item; for (var count = 0, loops = animals.length; count < loops; count++){ item = animals[count]; total += item.length; } console.log(total); //10
total
to calculate the amount, and assign zero to it. We also create the item
variable, in which, as the for loop is executed, the result of each iteration over the array of animals
stored. The count
variable is used as the loop counter, and loops
used to optimize iterations. We start the for
loop, iterate all the words in the array of animals
, assign the value of each of them to the item
variable, and add the lengths of the words to the accruing total.reduce()
method can do this much easier: var animals = ["cat","dog","fish"]; var total = animals.reduce(function(sum, word) { return sum + word.length; }, 0); console.log(total);
total
and assign it the value of the result obtained after applying reduce
to the array of animals
using two parameters: an anonymous inline function and a cumulative total. reduce
takes each element of the array, applies a function to it, and adds the result to the cumulative total, which is then passed to the next iteration. The substituted function takes two parameters: the cumulative total and the current word being processed from the array. To the length of the word function adds the current value of total
.reduce()
, so total
is a number. The reduce()
method will work without the second argument, but the result may differ from the expected. Try to determine for yourself what logic JavaScript uses when excluding total
.reduce()
method. Let's define a named function instead of an anonymous embedded function: var animals = ["cat","dog","fish"]; var addLength = function(sum, word) { return sum + word.length; }; var total = animals.reduce(addLength, 0); console.log(total);
reduce()
method. It takes two parameters: the function that is applied to each element of the array, and the initial value of the accruing total. In this case, we give the name of the new function addLength
initial value (zero) of the accruing total. The addLength()
function also takes two parameters: a cumulative total and a string value.map()
and reduce()
regularly, you can make your code cleaner, more flexible and easier to maintain. This will pave the way for you to use other functional approaches in JavaScript.map()
and reduce()
, other new methods have appeared in ECMAScript 5. Probably, improving the quality of the code and the pleasure of developing, which you feel, will far outweigh the temporary degradation of performance. Use functional approaches and measure the impact on performance in real projects, instead of thinking about whether map()
and reduce()
are needed in your application.Source: https://habr.com/ru/post/324342/
All Articles