?print-pdf
' Created by
map()
method creates a new array with the results of calling a provided function on every element in the calling array.
newArr = arr.map( callbackFn )
let input = ['a', 'b', 'c'];
let output = input.map((e,i,arr)=>{console.log(e, i, arr)})
//a 0 [ 'a', 'b', 'c' ]
//b 1 [ 'a', 'b', 'c' ]
//c 2 [ 'a', 'b', 'c' ]
let input = ['a', 'b', 'c', 'd'];
let output = input.map( e=>e.toUpperCase() )
console.log(`input: ${input}`);
console.log(`output: ${output}`);
//input: a,b,c,d
//output: A,B,C,D
let input = ['a', 'b', 'c', 'd'];
let output = input.map( function(e){
return e.toUpperCase()
} )
console.log(`input: ${input}`);
console.log(`output: ${output}`);
//input: a,b,c,d
//output: A,B,C,D
let input = ['a', 'b', 'c'];
let output = input.map((e,i)=>e.toUpperCase()+i)
console.log(output);
// [ 'A0', 'B1', 'C2' ]
// TASK: from 'cities' array generate a new array 'cityNames' which will contain only the names of the cities
let cities = [
{name: 'Sofia', population: 1_236_000},
{name: 'Plovdiv', population: 343_424 },
{name: 'Burgas', population: 202_766},
{name: 'Varna', population: 335_177},
];
// YOUR CODE HERE:
// TEST:
console.log(cityNames);
// EXPECTED OUTPUT:
// [ 'Sofia', 'Plovdiv', 'Burgas', 'Varna' ]
// TASK: from 'cities' array generate a new array 'bgCityNames' which will contain only the names
// of the cities, but mapped to their Bulgarian equivalent
let dict = {
'Sofia' : 'София',
'Plovdiv' : 'Пловдив',
'Burgas' : 'Бургас',
'Varna' : 'Варна'
}
let cities = [
{name: 'Sofia', population: 1_236_000},
{name: 'Plovdiv', population: 343_424 },
{name: 'Burgas', population: 202_766},
{name: 'Varna', population: 335_177},
];
// YOUR CODE HERE:
// TEST:
console.log(bgCityNames);
// EXPECTED OUTPUT:
// [ 'София', 'Пловдив', 'Бургас', 'Варна' ]
filter()
method creates a new array with all elements that pass the test implemented by the provided function.true
value.
let input = [0,1,2,3,4];
let output = input.filter( x=>x%2);
console.log(`input: ${input}`);
console.log(`output: ${output}`);
// TASK: filter only cities which population is greater than 340_000
let cities = [
{name: 'Sofia', population: 1_236_000},
{name: 'Plovdiv', population: 343_424 },
{name: 'Burgas', population: 202_766},
{name: 'Varna', population: 335_177},
];
// YOUR CODE HERE:
// TEST:
console.log(filtered);
// EXPECTED OUTPUT:
// [
// { name: 'Sofia', population: 1236000 },
// { name: 'Plovdiv', population: 343424 }
// ]
reduce()
method executes the provided reducer function on each element of the array, resulting in a single output value.
let value = arr.reduce(callbackFn)
let input = [1,2,3,4];
let output = input.reduce( (acc, curr)=> {
console.log(acc,curr)
return acc+curr;
} );
console.log(`output: ${output}`);
// OUTPUT
// 1 2
// 3 3
// 6 4
// output: 10
acc
initial value
let value = arr.reduce( (acc, curr)=>{...}, initialValue );
acc
is initialized the first time the callback is called.curr
to be initialized to the first value in the array.acc
is initialized to the first value in the array, and curr
is initialized to the second value in the array.
let input = [1,2,3,4];
let output = input.reduce( (acc, curr)=> {
console.log(acc,curr)
return acc+curr;
}, 0 );
console.log(`output: ${output}`);
// OUTPUT:
// 0 1
// 1 2
// 3 3
// 6 4
// output: 10
let input = [1,2,3,4];
let output = input.reduce( (acc, curr)=> curr%2==0?acc+curr:acc, 0);
console.log(`output: ${output}`);
let input = [1,2,3,4];
let output = input.filter(el=>el%2==0).reduce((acc,curr)=>acc+curr)
console.log(`output: ${output}`);
A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
that
trying
the
[1,2,3,4,5,6,7,8,9,10]
220
These slides are based on
customised version of
framework