javascript 8

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值