array method
forEach
const nums = [9, 8, 8, 7];
nums.forEach(function (n) {
console.log(n * n)
});//81,64,64,49
nums.forEach(function (el) {
if (el % 2 === 0) {
console.log(el)
}//8,8
})
MAP
Create a new array with the results of calling a callback on every element in the array
const texts = ['rofl', 'lol'];
const caps = texts.map(function(t){
return t.toUpperCase();
})
caps;//["ROFL","lol"]
Arrow
syntactically compact alternative to a regular function expression
const square = (x) => {
return x * x;
}
const square = function (x) {
return x *x;
}
//arrow function implicit returns( if just have one single expression)
const add = (a , b) => a + b
const isEven = num => (
return num % 2 ===0;
)
arrow function wrapup
const moview = [
{
title:'Amadeus',
score:99
},
{
title:'stand by me',
score:91
}
]
const newMovies = movies.map(function (movie) {
return `${movie.title} - ${movie.score / 10}`
})
const newMovies = movies.map((movie) => {
return ` ${movie.title} - ${movie.score / 10}`
})
const newMovies = movies.map((movie) => ` ${movie.title} - ${movie.score / 10}`
setTimeout, setInterval
setTimeout(() => {
console.log(" ...are u there?")
},3000)//3 milliseconds= 3 senconds
const id = setInternal(() => {
console.log(Math.random())
},2000);
clearInterval(id)
filter method
create a new array with all elements that pass the test implemented bt the provided function
const nums = [9, 8, 7, 6, 5,4,3,2,1];
const odds = nums.filter(n => {
return n % 2 ===1;// our callback returns true or false.
})//if it returns true, n is added to the filtered array
//[9,7,5,3,1]
const smallNums = nums.filter(n => n<5);
//[4,3,2,1]
movies.filter(m => {
return m.score>80
})
movies.filter(m =>m.score >80).map(m => m.title);
EVERY
test whether all elements in the array pass the provided function. It returns a boolean value
const words = ["dog", 'dig', 'log'};
words.every(word = > {
return word.length ===3;
})// true
words.every(word =>word[0] ==='d');//false
some
similar to every , but returns true if any of the array elements pass the test function
const words = ["dog", 'dig', 'log','cupcake'};
words.some(word = > {
return word.length >4;
})// true
words.some(word =>word[0] ==='d');//true
the notorious reduce method
executes a reducer function on each element of the array ,resulting in a single value
[3, 5, 7, 9, 11].reduce((accumulator, currentValue) => {
return accumulator + currentValue;
});
//callback accumularor currentValue returnvalue
first 3 5 8
second 8 7 15
third 15 9 24
fourth 24 11 35
const prices = [9,9,19.99,45.99]
const total = prices.reduce((total, price) => {
return total + price
})
total //9+9+19.99+45.99
prices.reduce((min, price) => {
if(price < min) {
return price;
}
})
const highestRated = movies.reduce((bestMovie, currMovie) =>{
if(currMovie.score > bestMovie.score) {
return currMovie;
}
return bestMovie;
})
arrow functions and 'this'
const person = {
firstName: 'amy',
lastName:'zhangsan',
fullName: function(){
return `${this.firstName} ${this.lastName}`
}
}
if use arrow function sometimes there is a problem because of the window