Function
objects, or functors). For example:
function greeting() { console.log('Hello World'); } // Invoking the function greeting(); // prints 'Hello World'
// We can add properties to functions like we do with objects greeting.lang = 'English'; // Prints 'English' console.log(greeting.lang);
const square = function(x) { return x * x; } // prints 25 square(5);
const foo = square; // prints 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(); } } // prints 'What's up?' greet('casual', formalGreeting, casualGreeting);
Array.prototype.map
, Array.prototype.filter
and Array.prototype.reduce
.
map()
method creates a new array with the result of calling the function to be passed for each element of the initial array. The map()
method takes each value of a callback function and creates a new array using these values.
map()
method takes three arguments: element
, index
and array
.
const arr1 = [1, 2, 3]; const arr2 = []; for(let i = 0; i < arr1.length; i++) { arr2.push(arr1[i] * 2); } // prints [ 2, 4, 6 ] console.log(arr2);
map
function:
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); } // prints [ 43, 21, 16, 23, 33 ] console.log(ages);
map
function:
const birthYear = [1975, 1997, 2002, 1995, 1985]; const ages = birthYear.map(year => 2018 - year); // prints [ 43, 21, 16, 23, 33 ] console.log(ages);
filter()
method creates a new array with all the elements that pass the test specified in the function being passed. The callback function passed to the filter()
method takes three arguments: element
, index
and array
.
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);
filter
function:
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 applies a function to each value of the array, reducing it to a single value. The reduce method takes two arguments:
initialValue
(the first argument at the first call of the function).accumulator
, currentValue
, currentIndex
, sourceArray
.
initialValue
parameter was passed, the accumulator
argument will be equal to the initialValue
argument and the currentValue
argument to the first element of the array.
initialValue
parameter was not passed, the accumulator
argument will be equal to the first element of the array, and the second element of the array will be taken as the currentValue
argument.
reduce
higher function:
const arr = [5, 7, 1, 8, 4]; const sum = arr.reduce(function(accumulator, currentValue) { return accumulator + currentValue; }); // prints 25 console.log(sum);
accumulator
argument stores the result of the previous action returned by the function, and the currentValue
takes the next value of the array. Upon completion, the result is stored in the variable sum
.
const arr = [5, 7, 1, 8, 4]; const sum = arr.reduce(function(accumulator, currentValue) { return accumulator + currentValue; }, 10); // prints 35 console.log(sum);
const arr = [5, 7, 1, 8, 4]; let sum = 0; for(let i = 0; i < arr.length; i++) { sum = sum + arr[i]; } // prints 25 console.log(sum);
map
method. We could build it ourselves, thereby creating our own 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; }); // prints [ 10, 6, 3, 4, 1 ] console.log(lenArray);
mapForEach
, which takes an array as arguments and a callback function fn
. This function is cyclically applied to each element of the array and calls the callback function fn
as part of the call to the newArray.push
function in each iteration.
fn
callback function takes the current element of the initial array and returns the length of this element, which is stored inside the new array newArray
. After the iteration is completed, the newArray
array newArray
returned as a result and is assigned by the lenArray
array.
Source: https://habr.com/ru/post/428612/