1. forEach

    const colors = ['red', 'blue', 'green']
colors.forEach(function (params) {
console.log(params)
})

2. map  重构-返回数组

    const porducts = [
{
name: 'cucumber',
type: 'vegetable',
quantity: 0,
price: 1
},
{
name: 'cucumber',
type: 'vegetable',
quantity: 30,
price: 10
},
{
name: 'cucumber',
type: 'fruit',
quantity: 10,
price: 16
},
] porducts.map(function (params) {
return params.price * 2
})

3. filter   过滤-返回数组

    let porducts = [
{
name: 'cucumber',
type: 'vegetable',
quantity: 0,
price: 1
},
{
name: 'cucumber',
type: 'vegetable',
quantity: 30,
price: 10
},
{
name: 'cucumber',
type: 'fruit',
quantity: 10,
price: 16
},
] porducts.filter(function (product) {
return product.type === 'fruit'
&& product.quantity > 0
&& product.price > 10
})

4. find  过滤-返回数组

    let users = [
{
name: 'jill',
},
{
name: 'alex',
id: 0
},
{
name: 'Bill',
},
{
name: 'alex',
id: 1
}
] const use = users.find(function (params) {
return params.name == 'alex'
})

5. every  一假即假-返回Boolean

    let computer = [
{ name: 'apple', ram: 16 },
{ name: 'IBM', ram: 4 },
{ name: 'ACer', ram: 32 },
] const com = computer.every(function (params) {
return params.ram > 16
})

6. some  一真即真-返回Boolean

    let computer = [
{ name: 'apple', ram: 16 },
{ name: 'IBM', ram: 4 },
{ name: 'ACer', ram: 32 },
] const comt = computer.some(function (params) {
return params.ram > 16
})

7. reduce   返回值

1. 比较值获得最大值
let porducts = [
{
name: 'cucumber',
type: 'vegetable',
quantity: 0,
price: 21
},
{
name: 'cucumber',
type: 'vegetable',
quantity: 30,
price: 20
},
{
name: 'cucumber',
type: 'fruit',
quantity: 10,
price: 16
},
] let prodList = porducts.reduce(function (first, last) {
if (first < last.price) {
first = last.price
}
return first
}, 0)
2. 值与值想加
let number = [10, 50, 30]
let sum = 0 let sumValue = number.reduce(function (sum2, number2) {
// console.log(sum2)
return sum2 + number2
}, 100)

ES6 数组方法 forEach map filter find every some reduce的更多相关文章

  1. ES6新增的常用数组方法(forEach,map,filter,every,some)

    ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log( ...

  2. js数组方法forEach,map,filter,every,some实现

    Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "fun ...

  3. 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight

    做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...

  4. 处理数组的forEach map filter的兼容性

    处理数组的forEach //forEach处理 if(!Array.prototype.forEach) { Array.prototype.forEach = function (callback ...

  5. ES6数组方法

    ES6数组方法 以下方法添加到了Array.prototype对象上(isArray除外) indexOf 类似字符串的indexOf()方法 stringObject.indexOf(searchv ...

  6. ES6 数组方法拓展

    ES6 数组方法拓展 1.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括E ...

  7. forEach, map, filter方法区别

    听说for循环已经成了菜鸟标配...? 瑟瑟发抖 赶紧找来资料补一补 1, forEach循环,循环数组中每一个元素并采取操作, 没有返回值, 可以不用知道数组长度 2, map函数,遍历数组每个元素 ...

  8. 数组的新方法 forEach some filter findIndex

    forEach  some  filter  findIndex这些都属于数组的新方法,都会对数组中的每一项,进行遍历,执行相关的操作: 只不过在循环的时候有些不一样 参考资料:https://wan ...

  9. 数组去重,排序,重复次数,两个数组合并,两个数组去重,map(),filter(),reduce()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. 使用Bootstrap的popover标签中嵌入插件,并且为插件注册事件实现Ajax与后台交互

    下午研究了一下bootstrap的popover写了个例子.如果项目很多地方都需要用到可以考虑封装成插件. javascript代码: <script type="text/javas ...

  2. Maven:浅析依赖(dependency)关系中 scope 的含义

    在 Pom4 中,dependency 元素中引入了 scope 元素,这是一个很重要的属性.在Maven 项目中 Jar 包冲突.类型转换异常的很大原因是由于 scope 元素使用不当造成的. sc ...

  3. x.append()增加不同维度的区别

    b=np.array([[7,2],[2,4],[3,6],[7,8],[9,10]])print(b)print(type(b)) # 结果显示为nunmpy 数组a=[]for i in rang ...

  4. 换个语言学一下 Golang (8)——指针

    定义 所谓指针其实你可以把它想像成一个箭头,这个箭头指向(存储)一个变量的地址. 因为这个箭头本身也需要变量来存储,所以也叫做指针变量. Go的指针不支持那些乱七八糟的指针移位.它就表示一个变量的地址 ...

  5. k8s--yml文件

  6. Linux下which、whereis、locate、find命令作用

    1 which 查看可执行文件的位置,也可以找到命令别名 2 whereis 查看文件的位置 3 locate 系统数据库查找文件位置,数据库大约每天更新一次 4 find 根据查找条件,搜寻硬盘查询 ...

  7. 长期作业:web框架源码剖析

    Tornado框架 1.1. 手动安装 1.2. 从简单的开始:分析红框部分的源码 Django框架

  8. react新旧生命周期

    React16.3.0之前生命周期 16.3开始建议使用新的生命周期

  9. CentOS 7 使用 firewalld 打开关闭防火墙与端口

    1.firewalld的基本使用启动: systemctl start firewalld关闭: systemctl stop firewalld查看状态: systemctl status fire ...

  10. SQL SERVER升级2017

    SQL SERVER升级2017 摘要 本文只介绍了SQL SERVER升级到2017(在简单环境下),分为开始升级前的检查事项,升级操作步骤,升级后对新实例的配置. 检查事项 1.检查当前版本是否可 ...