Javascript:forEach、map、filter、reduce、reduceRight、find

运行结果:

在这里插入图片描述

filter的使用

====================================================================

会过滤原数组,返回一个新的数组,过滤条件由filter中表达式决定

基础使用语法:

let array5 = array4.filter(value => 条件)

举例:

let array4 = [1, 2, 3, 4, 5, 6];

let array5 = array4.filter(value => value % 2 === 0)

console.log(array4)

console.log(array5)

运行结果:

在这里插入图片描述

reduce和reduceRight的使用

================================================================================

按照条件对原数组进行操作。

比如对原数组进行求和(求积),或在某数基础上对某一数组进行求和(求积)

基础使用语法:

let result = array6.reduce(函数,初始值,初始值下标,初始值下标数组)

举例:

let array6 = [1, 2, 3, 4];

function getSum(total, num) {

return total + num;

}

let result1 = array6.reduce(getSum);

let result2 = array6.reduce(getSum, 3);

let result3 = array6.reduce(getSum, 1, [1, 2, 3]);

console.log(result1, result2, result3);

运行结果:

在这里插入图片描述

reduceRight()方法的功能和reduce()功能是一样的,不同的是reduceRight()从数组的末尾向前将数组中的数组项做操作。

运算条件:求todo中finished为true的数据个数

finishedCount() {

return this.todos.reduce((total, todo) => total + (todo.finished ? 1 : 0), 0)

}

find和findIndex的使用

============================================================================

find:查询数组中第一个满足某条件的值

findIndex:查询数组中第一个满足某条件的值的下标

基础使用语法:

array7.find((value => 条件))

array7.findIndex((value => 条件))

举例:

let array7 = [

{name:‘1’,sex:‘女’,age:1},

{name:‘2’,sex:‘女’,age:2},

{name:‘3’,sex:‘女’,age:3},

{name:‘4’,sex:‘女’,age:4},

];

console.log(array7.find((value => value.name===‘4’)))

console.log(array7.findIndex((value => value.name===‘4’)))

运行结果:

在这里插入图片描述

keys,values,entries的使用

=================================================================================

ES6 提供三个新的方法 —— entries(),keys()和values() —— 用于遍历数组。它们都返回一个遍历器对象,可以用for…of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历

基础使用语法:

for(let index of 数组.keys()){

console.log(index) // 输出下标

}

for(let elem of 数组.values()){

console.log(elem) // 输出值

}

for(let [index,elem] of 数组.entries()){

console.log(index,elem) // 输出值和下标

}

举例:

let array8 = [1, 2, 3];

for(let index of array8.keys()){

console.log(index)

}

for(let elem of array8.values()){

console.log(elem)

}

总结

为了帮助大家更好温习重点知识、更高效的准备面试,特别整理了《前端工程师面试手册》电子稿文件。

内容包括html,css,JavaScript,ES6,计算机网络,浏览器,工程化,模块化,Node.js,框架,数据结构,性能优化,项目等等。

包含了腾讯、字节跳动、小米、阿里、滴滴、美团、58、拼多多、360、新浪、搜狐等一线互联网公司面试被问到的题目,涵盖了初中级前端技术点。

前端面试题汇总

JavaScript

性能

linux

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值