ES6 数组方法 forEach map filter find every some reduce
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的更多相关文章
- ES6新增的常用数组方法(forEach,map,filter,every,some)
ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log( ...
- js数组方法forEach,map,filter,every,some实现
Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "fun ...
- 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight
做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...
- 处理数组的forEach map filter的兼容性
处理数组的forEach //forEach处理 if(!Array.prototype.forEach) { Array.prototype.forEach = function (callback ...
- ES6数组方法
ES6数组方法 以下方法添加到了Array.prototype对象上(isArray除外) indexOf 类似字符串的indexOf()方法 stringObject.indexOf(searchv ...
- ES6 数组方法拓展
ES6 数组方法拓展 1.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括E ...
- forEach, map, filter方法区别
听说for循环已经成了菜鸟标配...? 瑟瑟发抖 赶紧找来资料补一补 1, forEach循环,循环数组中每一个元素并采取操作, 没有返回值, 可以不用知道数组长度 2, map函数,遍历数组每个元素 ...
- 数组的新方法 forEach some filter findIndex
forEach some filter findIndex这些都属于数组的新方法,都会对数组中的每一项,进行遍历,执行相关的操作: 只不过在循环的时候有些不一样 参考资料:https://wan ...
- 数组去重,排序,重复次数,两个数组合并,两个数组去重,map(),filter(),reduce()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
随机推荐
- 表单提交学习笔记(三)—利用Request.Files上传图片并预览
一.html页面如下 <div id="container"> <form id="myForm"> <p class=" ...
- SSO实现机制
引言 单点登录有许多开发商提供解决方案,本文以yale大学SSO开源项目CAS为例,介绍单点登录实现机制. 术语解释 SSO-Single Sign On,单点登录 TGT-Ticket Granti ...
- SpringbBoot之JPA批量更新
菜鸟学习,不对之处,还请纠正. 需要批量更新数据库的某些数据,项目使用的是JPA,刚对mybatis熟悉一点,又换成了JPA... 有点懵. 查询了一番之后,发现可以使用 In findByIdIn( ...
- HDU2476 String painter(DP)
题目 String painter 给出两个字符串s1,s2.对于每次操作可以将 s1 串中的任意一个子段变成另一个字符.问最少需要多少步操作能将s1串变为s2串. 解析 太妙了这个题,mark一下. ...
- 【开发笔记】- Java中关于HashMap的元素遍历的顺序问题
今天在使用如下的方式遍历HashMap里面的元素时 for (Entry<String, String> entry : hashMap.entrySet()) { MessageForm ...
- 如何使用GUID硬盘分区格式安装新windows系统
全局唯一标识分区表(GUID Partition Table,缩写:GPT)是一个实体硬盘的分区结构.目前硬盘格式有两种,一种MBR,另一个就是GUID.一般电脑买过来是windows7以上,比如wi ...
- selenium 滚动屏幕操作+上传文件
执行js脚本来滚动屏幕: (x,y)x为0 纵向滚动,y为0横向滚动 负数为向上滚动 driver.execute_script('window.scrollBy(0,250)') 上传文件: 1.导 ...
- ansible中的常用循环模块with_items
ansible中的循环模块有很多,不过with_items最为常用,且较为简单,循环模块最多的功能就是将重复性的任务简单化,如下例子所示: - hosts: all remote_user: root ...
- Eclipse经常出现未响应问题
修改eclipse.ini文件 -startupplugins/org.eclipse.equinox.launcher_1.5.0.v20180512-1130.jar--launcher.libr ...
- 团队项目-Beta冲刺
请大家在每次作业开头添加格式描述: 这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 <写上团队名称>(附上团队博客链接) 这 ...