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> ...
随机推荐
- CephFS 使用
原文:https://www.jianshu.com/p/c22ff79c4452 之前介绍了 RBD 的使用方法,有了 RBD,远程磁盘挂载的问题就解决了,但 RBD 的问题是不能多个主机共享一个磁 ...
- python 基础(集合)
#set里的元素是唯一的,即没有重复的,可以用set()函数,去数据的重复冗余 L = [1,1,1,2,4,5,6,7] S = set(L) print(S) #打印结果{1, 2, 4, 5, ...
- Thymeleaf前后端分页查询
分页查询是一个很常见的功能,对于分页也有很多封装好的轮子供我们使用. 比如使用mybatis做后端分页可以用Pagehelper这个插件,如果使用SpringDataJPA更方便,直接就内置的分页查询 ...
- IntelliJ idea鼠标移动到类上显示文档document(javadoc)内容
IntelliJ idea鼠标移动到类上显示文档document(javadoc)内容 Step 1:设置鼠标移动到类上自动显示Javadoc文档 step2:为jdk下载javadoc Step3: ...
- 十二、vue中watch原理
1.普通的watch 2.对象属性的watch: 对象和数组都是引用类型,引用类型变量存的是地址,地址没有变,所以不会触发watch.这时我们需要进行深度监听,就需要加上一个属性 deep,值为 ...
- 珠宝juelrye英语juelrye宝石
jewellery (usually uncountable, plural jewelleries) 1.(British spelling, Canadian) Collectively, per ...
- 英语wacche腕表
手表 (戴在手腕上的计时仪器) 手表,或称为腕表,是指戴在手腕上,用以计时/显示时间的仪器,手表在英语里watch源自中世纪wacche这一词汇. 手表通常是利用皮革.橡胶.尼龙布.不锈钢等材料,制成 ...
- 归并排序python实现源码
将开发过程经常用到的一些代码片段收藏起来,下面的资料是关于归并排序python实现的代码,应该能对码农们有一些用. def mergesort(arr): if len(arr) == 1: retu ...
- EFCore 中执行存储过程返回DataSet DataTable
在项目中由于需求,需要返回复杂的数据,需要执行存储过程,但是在DONETCORE2.0中,看官网文档执行的sql的有点操蛋,满足不了需求,就想到了ADO.NET 于是找资料,也没有合适的,就动手自己封 ...
- 【故障处理】 DBCA建库报错CRS-2566
[故障处理] DBCA建库报错CRS-2566 PRCR-1071 PRCR-1006 一.1 BLOG文档结构图 一.2 前言部分 一.2.1 导读和注意事项 各位技术爱好者, ...