Function
. Consider an example: function greeting() { console.log('Hello World'); } // greeting(); // 'Hello World'
// , greeting.lang = 'English'; // 'English' console.log(greeting.lang);
Object
, String
, Number
. Functions can be passed as parameters to other functions. Such functions transferred to others usually act as callback functions (callbacks). Functions can be assigned to variables, stored in arrays, and so on. That is why functions in JS are first class objects. const square = function(x) { return x * x; } // 25 square(5);
const foo = square; // 36 foo(6);
function formalGreeting() { console.log("How are you?"); } function casualGreeting() { console.log("What's up?"); } function greet(type, greetFormal, greetCasual) { if(type === 'formal') { greetFormal(); } else if(type === 'casual') { greetCasual(); } } // 'What's up?' greet('casual', formalGreeting, casualGreeting);
Array.prototype.map
, Array.prototype.filter
and Array.prototype.reduce
are functions of a higher order.map()
method creates a new array, calling, to process each element of the input array, a callback passed to it as an argument. This method takes each value returned by the callback and places it in the output array.map()
takes three arguments: element
(element), index
(index), and array
(array). Consider the examples. const arr1 = [1, 2, 3]; const arr2 = []; for(let i = 0; i < arr1.length; i++) { arr2.push(arr1[i] * 2); } // [ 2, 4, 6 ] console.log(arr2);
const arr1 = [1, 2, 3]; const arr2 = arr1.map(function(item) { return item * 2; }); console.log(arr2);
const arr1 = [1, 2, 3]; const arr2 = arr1.map(item => item * 2); console.log(arr2);
const birthYear = [1975, 1997, 2002, 1995, 1985]; const ages = []; for(let i = 0; i < birthYear.length; i++) { let age = 2018 - birthYear[i]; ages.push(age); } // [ 43, 21, 16, 23, 33 ] console.log(ages);
const birthYear = [1975, 1997, 2002, 1995, 1985]; const ages = birthYear.map(year => 2018 - year); // [ 43, 21, 16, 23, 33 ] console.log(ages);
filter()
method creates, on the basis of an array, a new array in which the elements of the original array fall within the conditions specified in the callback function passed to this method. This function accepts, as is the case with the map()
method, 3 arguments: element
, index
and array
.map()
method. const persons = [ { name: 'Peter', age: 16 }, { name: 'Mark', age: 18 }, { name: 'John', age: 27 }, { name: 'Jane', age: 14 }, { name: 'Tony', age: 24}, ]; const fullAge = []; for(let i = 0; i < persons.length; i++) { if(persons[i].age >= 18) { fullAge.push(persons[i]); } } console.log(fullAge);
const persons = [ { name: 'Peter', age: 16 }, { name: 'Mark', age: 18 }, { name: 'John', age: 27 }, { name: 'Jane', age: 14 }, { name: 'Tony', age: 24}, ]; const fullAge = persons.filter(person => person.age >= 18); console.log(fullAge);
reduce()
method processes each element of the array with a callback and places the result in a single output value. This method takes two parameters: a callback and an optional initial value ( initialValue
).accumulator
, currentValue
(current value), currentIndex
(current index), sourceArray
(source array).initialValue
parameter is provided to the initialValue
, then at the start of the method, the accumulator
will be equal to this value, and the first element of the array being processed will be written to the currentValue
.initialValue
parameter is not provided to the method, then the first element of the array will be written to the accumulator
, and the second will be written to the currentValue
. const arr = [5, 7, 1, 8, 4]; let sum = 0; for(let i = 0; i < arr.length; i++) { sum = sum + arr[i]; } // 25 console.log(sum);
reduce()
method without providing it with an initial value. const arr = [5, 7, 1, 8, 4]; const sum = arr.reduce(function(accumulator, currentValue) { return accumulator + currentValue; }); // 25 console.log(sum);
currentValue
, that is, the next element of the array, its accumulator
parameter turns out to contain the results of the previous operation, that is, what was returned from the function at the previous iteration. After the completion of this method, the final result falls into the constant sum
.reduce()
method. const arr = [5, 7, 1, 8, 4]; const sum = arr.reduce(function(accumulator, currentValue) { return accumulator + currentValue; }, 10); // 35 console.log(sum);
map()
. We can easily create such a method on our own, which will be expressed in the development of a higher order function. const strArray = ['JavaScript', 'Python', 'PHP', 'Java', 'C']; function mapForEach(arr, fn) { const newArray = []; for(let i = 0; i < arr.length; i++) { newArray.push( fn(arr[i]) ); } return newArray; } const lenArray = mapForEach(strArray, function(item) { return item.length; }); // [ 10, 6, 3, 4, 1 ] console.log(lenArray);
mapForEach
, which accepts an array and a callback function fn
. The mapForEach
function goes through the array in a loop and calls the fn
callback at each iteration of this loop.fn
takes the current string element of the array and returns the length of this element. What the fn
function returns is used in the newArray.push()
command and falls into an array returned by the mapForEach()
function. This array will eventually be written to the lenArray
constant.Source: https://habr.com/ru/post/428570/
All Articles