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> ...
随机推荐
- IDEA和Eclipse启动优化
昨天对比了下IDEA和Eclipse的启动速度,发现IDEA启动真的是好慢啊!!! 电脑配置:8G win7 IDEA启动配置 -Xms1024m -Xmx1024m -Xmn500m -XX:Met ...
- 04、MySQL—数据基础操作
1.插入操作 本质含义:将数据以SQL的形式存储到指定的数据表(字段)里面 基本语法:向表中指定字段插入数据 Insert into 表名[(字段列表)] values(对应字段列表) 范例:向表中插 ...
- pandas-05 map和replace操作
# pandas-05 map和replace操作 map可以做一个映射,对于操作大型的dataframe来说就非常方便了,而且也不容易出错.replace的作用是替换,这个很好理解. import ...
- day 09 预科
目录 函数 定义函数 函数定义的三种形式 空函数 有参函数(有参数()的函数) 无参函数 函数的返回值 函数的参数 形参 位置形参 实参 位置实参 关键字实参 函数 def twoSum(nums,t ...
- PHP Lumen Laravel 解决validate方法自定义message无效的问题
/** * 由于 \Laravel\Lumen\Routing\ProvidesConvenienceMethods::validate 在验证不通过时, * 抛出 \Illuminate\Valid ...
- CentOS环境安装python3,pip
安装python3(编译安装) 1. 下载python安装包到指定目录 cd /usr/local/src 这里的目录如果不存在可以自己建,当然也可以自己放在自己想放的位置,只要等会儿安装的时候能找到 ...
- MySQL/MariaDB数据库的查询缓存优化
MySQL/MariaDB数据库的查询缓存优化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL架构 Connectors(MySQL对外提供的交互接口,API): ...
- 第一次使用Git(常用的dos命令整理)
在使用git的过程中,有许多dos命令也要会用才行 Git 工具分类 命令行 bash.cmd.power shell GUI Git GUI.GitHub Desktop IDE 集成 Visual ...
- Proxmox初步了解
Proxmox不分主从,所有节点同步信息 创建集群 pvecm(可通过web界面创建.添加至集群) pvecm create cluster01 pvecm status 添加节点 pvecm add ...
- Linux中在vim/vi模式下对文本的查找和替换
查找: 1.vim filename 进入一般模式下 2.查找和替换方法 /word 向下查找word 的字符串 例如 /chengtingting 向下查找字符chengtingt ...