forEach()的使用:

基础使用语法:

array.forEach(function(value, index, array){console.log(value,index,array)
})

其中,回调函数中,第一个参数value是当前遍历的值,第二个参数index是当前遍历的下标,第三个参数array是数组本身
举例:

let array = [1, 2, 3];
array.forEach(function(value, index, array){console.log(value,index,array)
})

运行结果:

map的使用

会返回一个新的数据,其中值有map中的表达式决定。

基础使用语法:

let array3 = array2.map(value => 条件)
let res = array2.map(function(item,index,arr){return 条件;
})

举例:

let array2 = [4, 5, 6];
let array3 = array2.map(value => value * 2)
console.log(array2)
console.log(array3)

运行结果:

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)
}
for(let [index,elem] of array8.entries()){console.log(index,elem)
}

运行结果:

every和some的使用

判断数组中的所有或者任一数据是否满足某条件,如果满足返回true,否则返回false
基础使用语法:

console.log(array9.some(function (value, index, array) {return 判断条件;
}))
console.log(array9.every(function (value, index, array) {return 判断条件;
}))

举例:

let array9 = [1, 2, 3, 4, 5];
console.log(array9.some(function (value, index, array) {return value % 2 === 0;
}))
console.log(array9.every(function (value, index, array) {return value % 2 === 0;
}))

运行结果:

Javascript:forEach、map、filter、reduce、reduceRight、find、findIndex、keys、values、entries、every、some的使用相关推荐

  1. js数组中forEach/some/every/map/filter/reduce的区别

    2019独角兽企业重金招聘Python工程师标准>>> // js数组中forEach/some/every/map/filter/reduce的区别// 1. foreach:就是 ...

  2. python map filter reduce

    本文记录python中,map,filter,reduce函数的用法. 参考链接: http://www.python-course.eu/lambda.php map map(func, seq) ...

  3. 基本函数input() print() map() filter() reduce()和lambda()算子-operater用法

    #输入逗号分割的两个数字--input输入的内容,默认为字符类型 x,y =input("input:").split(",") print(x,y)#输入的多 ...

  4. Python 进阶之路 (五) map, filter, reduce, zip 一网打尽

    简洁的内置函数 大家好,我又回来了,今天我想和大家分享的是Python非常重要的几个内置函数:map,filter,reduce, zip. 它们都是处理序列的便捷函数.这很大程度上归功于函数式编程的 ...

  5. 前端手动封装数组的foreach,map,filter,every,some,Reduce,reduceRight方法

    我是歌谣 放弃很容易 但坚持一定很酷 公众号小歌谣 谢谢关注 前言 Foreach Array.prototype.myForEach = function (cb) {var _arr = this ...

  6. swift_046(Swift map,filter, reduce方法)

    首先,咱们说说 map函数如何使用. 1 2 3 let numbers = [1,2,3,4] let result = numbers.map { $0 + 2 } print(result)   ...

  7. 不生成新数组的迭代器方法:forEach()every()some()reduce()reduceRight()

    一.forEach():接受一个函数作为参数, 对数组中的每个元素使用该函数. function square(num) {alert(num + " : " + num * nu ...

  8. python十九:map,filter,reduce函数

    # 处理序列中的每个元素,得到的结果是一个'列表',该'列表'元素个数及位置与原来一样 def map_practice(func, lt_num):lt_new = []for i in lt_nu ...

  9. map,filter,reduce函数总结

    #map:处理序列中的每一个元素,得到的结果是一个'列表(迭代器)',该列表元素个数及位置与原来一样#map处理的对象可以是列表,列表中的内容可以是字符串,数字,..也可以只处理一个独立的字符串 #f ...

  10. 尾递归调用 高阶函数 map filter reduce

    #!/user/bin/env python# -*- coding:utf-8 -*-# 1.函数递归调用,函数返回值如果是另一个函数,而不是一个确切值,返回的则是这个函数的地址,需要我们加上()后 ...

最新文章

  1. 16 导出pcb各网络的布线长度_设计PCB流程
  2. Django ModelForm操作及验证
  3. Kali Linux 2016.2初体验使用总结
  4. Intellij idea快速查看Java类字节码
  5. msp430g2553串口接受数据_MSP430G2553串口通信
  6. asp.net中生命周期的浅析
  7. 数据分析该分析什么?
  8. Spring Security 认证执行流程
  9. linux通过bg后台执行作业
  10. STM32中断与事件
  11. 蔚来召回4803辆ES8电动汽车 自燃原因找到了?
  12. LIRe 源代码分析 5:提取特征向量[以颜色布局为例]
  13. PreferenceActivity详解
  14. vue 基于网易云API实现二维码的登录
  15. 推荐克莱夫·汤普森《天才程序员》
  16. 快手用户群体分析_报告称快手用户分布与移动互联网人群分布一致
  17. 解决用U盘重装Mac系统中电脑无法识别U盘的问题
  18. foxmail 登陆gmail报密码错误
  19. 【Linux命令】mergecap命令的用法
  20. Flutter中使用connectivity实现网络检测

热门文章

  1. 16.Linux/Unix 系统编程手册(上) -- 扩展属性
  2. 2.nginx 配置
  3. 22.搜索大纲及重定向(Search Synonyms and Re-directs)
  4. 4. 正则表达式(4)
  5. linux多播 多个接收方,在同一端口上接收多个多播源 – C,Linux
  6. Struts中 s checkboxlist 的用法
  7. jxl 导入导出Excel(有模板)
  8. 深入理解Java虚拟机 学习总结
  9. Linux下使用shell脚本远程登录主机(Ubuntu CentOS)
  10. Mine Number(搜索,暴力) ACM省赛第三届 G