filter
method, you receive in response another array, of the same length or less than the initial one. It contains a subset of elements from the source array that satisfy the specified conditions. var animals = ["cat","dog","fish"]; var threeLetterAnimals = []; for (let count = 0; count < animals.length; count++){ if (animals[count].length === 3) { threeLetterAnimals.push(animals[count]); } } console.log(threeLetterAnimals); // ["cat", "dog"]
filter
method makes the code much cleaner and simpler. For example: var animals = ["cat","dog","fish"]; var threeLetterAnimals = animals.filter(function(animal) { return animal.length === 3; }); console.log(threeLetterAnimals); // ["cat", "dog"]
filter
method, we directly linked the filter results to the second array. Passing the filter
an anonymous in-line function that returns true
if the length of the operated value is three.filter
method works like this: passes through each element of the array and applies a test function to it (test function). If the function returns true
, the filter
method returns an array containing this element. Other items are skipped.filter
does, you can understand its principle of operation from the code.count
and the different states that the threeLetterAnimals
array threeLetterAnimals
when looping through the source array are additional states that need to be monitored. The filter
method has saved us from the loop and count variable. And we do not change many times the value for the new array, as in the first case. We defined it once and associated it with the value resulting from applying the filter
condition to the original array.const
declarations and anonymous inline arrow functions. This is thanks to EcmaScript 6 (ES6), which is natively supported by most browsers and JavaScript engines. const animals = ["cat","dog","fish"]; const threeLetterAnimals = animals.filter(item => item.length === 3); console.log(threeLetterAnimals); // ["cat", "dog"]
filter
method. It might look like this: const animals = ["cat","dog","fish"]; function exactlyThree(word) { return word.length === 3; } const threeLetterAnimals = animals.filter(exactlyThree); console.log(threeLetterAnimals); // ["cat", "dog"]
filter
as a condition.map
and reduce
. When creating chains of methods, you can use this combination to write very clean code that performs fairly complex functions.map
method goes through each element of the array, converts it in accordance with the function, and returns a new array of the same length, but with transformed values. const animals = ["cat","dog","fish"]; const lengths = animals.map(getLength); function getLength(word) { return word.length; } console.log(lengths); //[3, 3, 4]
reduce
method passes through an array and performs a series of operations. The intermediate result of each of them passes to the adder. Upon completion of array processing, the method gives the final result. In our case, you can use the second argument to initialize the adder to 0. const animals = ["cat","dog","fish"]; const total = animals.reduce(addLength, 0); function addLength(sum, word) { return sum + word.length; } console.log(total); //10
map
, reduce
and filter
it will look something like this: const animals = ["cat","dog","fish"]; let threeLetterAnimalsArray = []; let threeLetterAnimals; let item; for (let count = 0; count < animals.length; count++){ item = animals[count]; if (item.length === 3) { item = item.charAt(0).toUpperCase() + item.slice(1); threeLetterAnimalsArray.push(item); } } threeLetterAnimals = threeLetterAnimalsArray.join(""); console.log(threeLetterAnimals); // "CatDog"
let
or const
.map
, reduce
and filter
methods, passing the results from one to another: const animals = ["cat","dog","fish"]; function studlyCaps(words, word) { return words + word; } function exactlyThree(word) { return (word.length === 3); } function capitalize(word) { return word.charAt(0).toUpperCase() + word.slice(1); } const threeLetterAnimals = animals .filter(exactlyThree) .map(capitalize) .reduce(studlyCaps); console.log(threeLetterAnimals); // "CatDog"
studlyCaps
, exactlyThree
and capitalize
. You can pass them directly to map
, reduce
and filter
within the same unbroken chain. First, using exactlyThree
filter the source array. Pass the result to capitalize
. And already its result is processed with the help of studlyCaps
. The final result is assigned directly to the variable threeLetterAnimals
. Without cycles and intermediate states, without touching the source array.filter
method will probably work a little slower than the for
loop until browsers and JS engines are optimized for new methods of working with arrays ( jsPerf ).filter
method is unlikely to be a bottleneck. But the only way to make sure of this is to try it yourself.filter
in a real situation is much slower than the cycle, if it affects the users, then you know where and how you can optimize. And as far as the JS engines are finished, the performance will only increase.filter
method, you will not change the state of the array as you calculate. Each time you return a new array, and the original will remain intact.Source: https://habr.com/ru/post/324172/
All Articles