indexOf()
method." Something like this recommendation I met at one of the courses when I was learning JavaScript. The recommendation is quite normal, nothing bad can be said about it.indexOf()
method returns the first index by which an element can be found in the array. This means that if we plan to use this index in the program, the indexof()
method is excellent for searching elements in arrays.true
or false
. In such cases, I recommend using not the indexOf()
method, but the includes()
method, which returns a boolean value. Consider an example: 'use strict'; const characters = [ 'ironman', 'black_widow', 'hulk', 'captain_america', 'hulk', 'thor', ]; console.log(characters.indexOf('hulk')); // 2 console.log(characters.indexOf('batman')); // -1 console.log(characters.includes('hulk')); // true console.log(characters.includes('batman')); // false
filter()
method is a very useful tool. He, on the basis of one array, creates another array containing the elements of the source array corresponding to the given condition. As can be understood from the name of this method, it is intended for filtering arrays, during which arrays are usually obtained that are shorter than the original ones.filter()
method, since the array that it forms will contain only one element. If we are interested in an array element with a unique value, then we are going to work with a single value, and to represent such a value, the array is not needed.filter()
method, then it turns out that in order to form a list of the elements corresponding to the condition specified when it was called, you will have to look through the entire array. Moreover, imagine that there are hundreds of elements in an array that satisfy a given condition. This will result in the resulting array being rather large.find()
method. When called, it is passed a callback describing a condition very similar to that used with the filter()
method, but the find()
method returns only the first element that matches the condition. In this case, this method stops working immediately after it finds such an element. The entire array to him, in the end, do not have to browse. 'use strict'; const characters = [ { id: 1, name: 'ironman' }, { id: 2, name: 'black_widow' }, { id: 3, name: 'captain_america' }, { id: 4, name: 'captain_america' }, ]; function getCharacter(name) { return character => character.name === name; } console.log(characters.filter(getCharacter('captain_america'))); // [ // { id: 3, name: 'captain_america' }, // { id: 4, name: 'captain_america' }, // ] console.log(characters.find(getCharacter('captain_america'))); // { id: 3, name: 'captain_america' }
indexOf()
and includes()
.find()
method, as an argument, takes a callback and returns an array element. Is it possible to call the find()
method the most successful solution if we need to find out if the array contains some value or not? Perhaps - no, because this method returns the value of an element of the array, rather than a logical value.some()
method, which returns a boolean value. 'use strict'; const characters = [ { id: 1, name: 'ironman', env: 'marvel' }, { id: 2, name: 'black_widow', env: 'marvel' }, { id: 3, name: 'wonder_woman', env: 'dc_comics' }, ]; function hasCharacterFrom(env) { return character => character.env === env; } console.log(characters.find(hasCharacterFrom('marvel'))); // { id: 1, name: 'ironman', env: 'marvel' } console.log(characters.some(hasCharacterFrom('marvel'))); // true
reduce()
method cannot be attributed to simple to understand. However, if what can be done with it is done in two steps, using the filter()
and map()
methods that are chained, it seems that something in this approach is wrong.filter()
method involves viewing the entire array and creating a new, filtered array. After the second pass, performed by the map()
method, again, a new array is created, which contains the results of the transformation of the array elements obtained after the filter()
method works. As a result, in order to enter the finished array, two methods are used. Each method has its own callback, while creating such an operation using the filter()
method creates an array with which we can no longer work.reduce()
method. The result will be the same, and the code will turn out better. This method allows you to filter the elements of interest to us and add them to the battery. A battery can be a numeric variable that stores, say, the sum of the elements of an array, it can be an object, a string, or an array in which we can accumulate the elements we need.map()
method, I would advise using the reduce()
method with an array as a battery. In the following example, we filter the elements of the array, which are objects, by the value of the env
field, and perform their conversion. 'use strict'; const characters = [ { name: 'ironman', env: 'marvel' }, { name: 'black_widow', env: 'marvel' }, { name: 'wonder_woman', env: 'dc_comics' }, ]; console.log( characters .filter(character => character.env === 'marvel') .map(character => Object.assign({}, character, { alsoSeenIn: ['Avengers'] })) ); // [ // { name: 'ironman', env: 'marvel', alsoSeenIn: ['Avengers'] }, // { name: 'black_widow', env: 'marvel', alsoSeenIn: ['Avengers'] } // ] console.log( characters .reduce((acc, character) => { return character.env === 'marvel' ? acc.concat(Object.assign({}, character, { alsoSeenIn: ['Avengers'] })) : acc; }, []) ) // [ // { name: 'ironman', env: 'marvel', alsoSeenIn: ['Avengers'] }, // { name: 'black_widow', env: 'marvel', alsoSeenIn: ['Avengers'] } // ]
Source: https://habr.com/ru/post/422091/
All Articles