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. Nas 系统的虚拟化方案

    Nas 系统的虚拟化方案 https://zhuanlan.zhihu.com/p/55025102 对搞技术的人来说,Nas 是个理想的玩具,既然是程序员用的 Nas ,自然要专业一点,不能像小白一 ...

  2. eyoucms 模板

    https://www.oschina.net/p/eyoucms 下载模板 https://www.eyoucms.com/doc/operation/ 学习手册

  3. 深入了解Cookie和Session

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  4. SparkStreaming+kafka Receiver模式

    1.图解 2.过程 1.使用Kafka的High Level Consumer API 实现,消费者不能自己去维护消费者offset,而且kafka也不关心数据是否丢失. 2.当向zookeeper中 ...

  5. C#-NLog记录日志

    Nuget获取包 配置文件 <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns= &quo ...

  6. CentOS7安装VNC

    #安装 yum -y install tigervnc-server 将配置表复制到etc .service 修改配置文件 vim /etc/systemd/system/vncserver@\:.s ...

  7. 【Mybatis异常】Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

    一.错误原因分析 从错误提示可以看出:实际传入的参数大于sql中待设置的参数,也就是sql中的?少于参数或?根本没有产生原因:  ?号被单引号包围 如: sql += " and artic ...

  8. python写一些简单的tcp服务器和客户端

    代码贴上,做个记录 TcpClient # -*- coding:utf-8 -*- import socket target_host = "127.0.0.1" #服务器端地址 ...

  9. mysql 增加列

    alter table t_zhaosheng_chengji_is_finish add season_id int(4) default '1' not null COMMENT '招生季ID';

  10. Local CubeMap实现玻璃折射

    这个方法来自于Arm公司Cave Demo中的冰雕效果 原文提供了一种计算折射向量的方法, 这里用个更简单的方式尝试发现效果也不错: float3 v = -normalize(_WorldSpace ...