Es6 utilities
Filter
Info
The filter() method take an arg function with array element like parameter. The function return a booleanfilter. An second parameter may be used for element indice. Filter return an array.
Simple Example
const users = ['John', 'Marc', 'Mattew', 'Peter', 'Paul'];
const result = users.filter(user => user.length > 4);
// return name length > 4
// result return: Array["Mattew", "Peter"]
const result = users.filter((user, i) => i > 2);
// return indice > 2
// result return: Array["Peter", "Paul"]
Filter example with an object
Output all true data from user object
Map
Info
map take an arg function with array element like parameter. The function must return the new value. An second parameter may be used for element indice. Map return an array.
Simple Example
const users = ['John', 'Marc', 'Mattew', 'Peter', 'Paul'];
const result = users.filter(user => user.length > 4);
// result return: Array["Mattew", "Peter"]
const result = users.filter((user, i) => i > 2);
Reduce
Info
The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
Simple Example
const array1 = [1, 2, 3, 4];
const reducer = array1.reduce((acc, element) => acc + element);
// return 10
const reducer2 = array1.reduce((acc, element, i) => acc + element + i);
// return 16
Reduce Exemple
Intial data
const pilots = [
{
id:10,
name: "Peo Dameron",
years: 30,
},
{
id:2,
name: "Temin Wexley",
years: 50,
},
{
id:41,
name: "Tallissan Lintra",
years: 13,
},
{
id:99,
name: "Ello Asty",
years: 5,
},
];