1. Explain the difference between forEach, map, filter, and reduce.
forEach:
forEach: Iterate an array elements. We use forEach
only with arrays. It takes a callback function with elements, index
parameter and array itself. The index and the array optional.
Some forEach example given below.
i.
let sum = 0;
const numbers = [1,2,3,4,5];
numbers.forEach(num => console.log(num));
output:
ii. sum of array element
let sum = 0;
const numbers = [1,2,3,4,5];
numbers.forEach(num => sum += num);
console.log(sum);
output: 15
iii. Make all element Capital
const countries = ["United State", "Japan","HongKong","Afganistan","London","India"];
const capitalLetters = countries.forEach((cap)=> console.log(cap.toUpperCase()));
output:
UNITED STATE
JAPAN
HONGKONG
AFGANISTAN
LONDON
INDIA
map
map: Iterate an array elements and modify the
array elements. It takes a callback function with elements, index ,
array parameter and return a new array.
i. find the square of all array element
const num = [1,2,3,4,5];
const numSquare = num.map((num1)=> num1 * num1);
console.log(numSquare);
ii. make all element uppercase
const names = ["WhoAmI","Anonymous","Root","unknown"];
const namesToUpperCase = names.map((upper)=> console.log(upper.toUpperCase()));
output: WHOAMI
ANONYMOUS
ROOT
UNKNOWN
iii. count first 3 three letters
const names = ["WhoAmI","Anonymous","Root","unknown"];
const count_first_three_letters = names.map((upperCase_3) => {
return upperCase_3.toUpperCase().slice(0,3)
});
console.log(count_first_three_letters);
output: [ 'WHO', 'ANO', 'ROO', 'UNK' ]
filter
Filter: Filter out items which full fill filtering conditions and return a new array.
i. Filter countries containing "land"
const countries = ['ALBENIA', 'BOLIVIA', 'CANADA', 'ETHIOPIA', 'FINLAND', 'GERMANY', 'IRELAND'];
// Filter countries containing "land"
const countries_containing_land = countries.filter((fil) =>
fil.includes("LAND"));
console.log(countries_containing_land);
output:
Note: Keep in mind JavaScript is Case Sensitive.
ii. Find countries that end with "ia"
const countries = ['ALBENIA', 'BOLIVIA', 'CANADA', 'ETHIOPIA', 'FINLAND', 'GERMANY', 'IRELAND'];
const countriesEndWithIa = countries.filter((country) => country.endsWith('IA'));
console.log(countriesEndWithIa);
output: [ 'ALBENIA', 'BOLIVIA', 'ETHIOPIA' ]
iii. Find country that have six letters
const countries = ['ALBENIA', 'BOLIVIA', 'CANADA', 'ETHIOPIA', 'FINLAND', 'GERMANY', 'IRELAND'];
const countries_have_six_letters = countries.filter((letter) => letter.length === 6);
console.log(countries_have_six_letters);
iv. Find score greater than 80
const scores = [
{
name: "London", score: 95
},
{
name: "Japan",score: 75
},
{
name: "HongKong", score: 50
},
{
name: "Brazil", score: 85
},
{
name: "Malaysia", score: 100
}
];
const score_greater_than_Eighty = scores.filter((sc) => sc.score > 80);
console.log(score_greater_than_Eighty);
output:
[
{ name: 'London', score: 95 },
{ name: 'Brazil', score: 85 },
{ name: 'Malaysia', score: 100 }
]
reduce
reduce: Reduce takes a callback function. The
call back function takes accumulator, current, and optional initial
value as a parameter and returns a single value. It is a good practice
to define an initial value for the accumulator value. If we do not
specify this parameter, by default accumulator will get array first value. If our array is an empty array, then Javascript will throw an error.
i. add all the array element
const numbers = [1,2,3,4,5];
const sum = numbers.reduce((acc,cur) => acc + cur, 0);
console.log(sum);
output: 15
every
every: Check if all the elements are similar in one aspect. It returns boolean
const countries = ['ALBENIA', 'BOLIVIA', 'CANADA', 'ETHIOPIA', 'IRELAND'];
const allStr = countries.every((len) => typeof len === "string");
console.log(allStr);
output: true