?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' ]
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:
const cityNames = cities.map(city=>city.name);
// TEST:
console.log(cityNames);
// 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:
// [ 'София', 'Пловдив', 'Бургас', 'Варна' ]
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:
const bgCityNames = cities.map(city=>dict[city.name])
// TEST:
console.log(bgCityNames);
// OUTPUT:
// [ 'София', 'Пловдив', 'Бургас', 'Варна' ]
filter() method creates a new array containing only the elements from the original array that meet a specific condition.
const input = [0,1,2,3,4];
const output = input.filter( x=>x%2===0);
console.log(`input: ${input}`);
console.log(`output: ${output}`);
// TASK: filter only cities which population is greater than 340_000
const 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(populatedCities);
// EXPECTED OUTPUT:
// [
// { name: 'Sofia', population: 1236000 },
// { name: 'Plovdiv', population: 343424 }
// ]
const 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:
const populatedCities = cities.filter(city=>city.population>340_000)
// TEST:
console.log(populatedCities);
// OUTPUT:
// [
// { name: 'Sofia', population: 1236000 },
// { name: 'Plovdiv', population: 343424 }
// ]
reduce() method executes the provided callback function on each element of the array to reduce the array to a single output value.
let value = arr.reduce(callbackFn, [initialValue])
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
let value = arr.reduce( (accumulator, current)=>{...}, [initialValue] );
(accumulator, current) => { ... }: The callback function applied to each element.
accumulator is initialized the first time the callback is called.accumulator is initialized to the first value in the array, and current 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}`);
array.forEach(callback(currentValue, index, array) {
// Code to execute for each element
}, thisArg);
const numbers = [1, 2, 3, 4, 5];
// Using forEach to log each number and its index:
numbers.forEach(function(number, index) {
console.log(`Index ${index}: ${number}`);
});
// Using an arrow function
numbers.forEach((number, index) => {
console.log(`Index ${index}: ${number}`);
});
//TASK: calculate the total sales from an array of sales objects using the forEach method
// GIVEN
const sales = [
{ product: 'Laptop', amount: 1000 },
{ product: 'Phone', amount: 500 },
{ product: 'Tablet', amount: 300 }
];
// YOUR CODE HERE
// TEST
console.log(totalSales);
// EXPECTED OUTPUT:
// 1800
//TASK: calculate the total sales from an array of sales objects using the forEach method
// GIVEN
const sales = [
{ product: 'Laptop', amount: 1000 },
{ product: 'Phone', amount: 500 },
{ product: 'Tablet', amount: 300 }
];
// YOUR CODE HERE
let totalSales = 0;
sales.forEach(sale => totalSales += sale.amount);
// TEST
console.log(totalSales);
// OUTPUT:
// 1800
//TASK: You are given an array of user objects.
//Add an isAdmin property to each user object based on the user's role within the array of user objects.
// GIVEN
const users = [
{ name: 'Maria', role: 'user' },
{ name: 'Ivan', role: 'admin' },
{ name: 'Stoyan', role: 'user' }
];
// YOUR CODE HERE
// TEST
console.log(users);
// EXPECTED OUTPUT:
// [
// { name: 'Maria', role: 'user', isAdmin: false },
// { name: 'Ivan', role: 'admin', isAdmin: true },
// { name: 'Stoyan', role: 'user', isAdmin: false }
// ]
//TASK: You are given an array of user objects.
//Add an isAdmin property to each user object based on the user's role within the array of user objects.
// GIVEN
const users = [
{ name: 'Maria', role: 'user' },
{ name: 'Ivan', role: 'admin' },
{ name: 'Stoyan', role: 'user' }
];
// YOUR CODE HERE
users.forEach(user => {
user.isAdmin = user.role === 'admin';
});
// TEST
console.log(users);
// EXPECTED OUTPUT:
// [
// { name: 'Maria', role: 'user', isAdmin: false },
// { name: 'Ivan', role: 'admin', isAdmin: true },
// { name: 'Stoyan', role: 'user', isAdmin: false }
// ]
find() method returns the first element in the array that satisfies the condition specified by the provided callback function.
arr.find(callbackFn)
undefined.
const input = [1, 2, 3, 4, 5];
const output = input.find(x => x > 3);
console.log(`input: ${input}`);
//output: 4
// TASK: Find the first city with a population greater than 500,000
const 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(firstLargeCity);
// EXPECTED OUTPUT:
// { name: 'Sofia', population: 1236000 }
const 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:
const firstLargeCity = cities.find(city => city.population > 500_000);
// TEST:
console.log(firstLargeCity);
// OUTPUT:
// { name: 'Sofia', population: 1236000 }
sort() method sorts the elements of an array in place and returns the sorted array.
arr.sort()
arr.sort(compareFn)
[...arr].sort(compareFn)
a and b, which represent two elements being compared.compare(a, b) < 0, a is placed before b.compare(a, b) > 0, a is placed after b.compare(a, b) === 0, the order of a and b remains unchanged.
const input = [13, 1, 4, 1, 15, 9];
input.sort()
console.log(input); // [ 1, 1, 13, 15, 4, 9 ]
const input = [13, 1, 4, 1, 15, 9];
input.sort((a,b)=>a-b);
console.log(input); // [ 1, 1, 4, 9, 13, 15 ]
// TASK: Sort the cities array in descending order of population
const 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(sortedCities);
// EXPECTED OUTPUT:
// [
// { name: 'Sofia', population: 1236000 },
// { name: 'Plovdiv', population: 343424 },
// { name: 'Varna', population: 335177 },
// { name: 'Burgas', population: 202766 }
// ]
const 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:
const sortedCities = cities.sort((a, b) => b.population - a.population);
// TEST:
console.log(sortedCities);
// OUTPUT:
// [
// { name: 'Sofia', population: 1236000 },
// { name: 'Plovdiv', population: 343424 },
// { name: 'Varna', population: 335177 },
// { name: 'Burgas', population: 202766 }
// ]
These slides are based on
customized version of
framework