当前位置:网站首页>reduce()方法的学习和整理
reduce()方法的学习和整理
2022-08-05 05:24:00 【十三月呀】
今天看文章的时候看到reduce方法去做循环比较是较为优雅的写法,遂学习了一波,现整理如下。
文章链接:https://www.jianshu.com/p/e375ba1cfc47
1. 语法
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
reduce() 可以作为一个高阶函数,用于函数的 compose。
注意: reduce() 对于空数组是不会执行回调函数的。
arr.reduce(callback,[initialValue])
callback详解:
- previousValues: 上一次调用回调函数返回的值,第一次是
initialValue的值,如何未设置initialValue值,是数组的第一个元素的值 - currentValue: 数组当前被处理的值,未设置
initialValue,为数组的第二个元素的值。 - index:当前元素在数组中的索引。默认值为1,设置
initialValue则为initialValue的值 - array:调用reduce方法的数组
- initialValue: 第一次调用callback的第一个参数
2. 实例
var arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
console.log(prev, cur, index);
return prev + cur;
})
console.log(arr, sum);
打印结果:
1 2 1
3 3 2
6 4 3
[1, 2, 3, 4] 10
3. 其他语法
if in
if ( cur in pre ) {
pre[cur]++
}
if in 用法是判断属性是否属于对象
上述代表的是在 pre 中是否有 cur 属性
注: in 我一般用于 for in 没使用过 if in,还是见识少,要多看书和文章。
includes
if ( !pre.includes(cur) ) {
return pre.concat(cur)
}
ncludes() 方法用于判断字符串是否包含指定的子字符串。
如果找到匹配的字符串则返回 true,否则返回 false。
includes() 方法区分大小写。
故上述代码是用来判断 cur 中是否包含字符串 pre
4. 讨论
和后台男朋友说到元素匹配数组,男朋友告诉我后台有个 contains 方法,js里面可能也有,搜索了一下发现是jQuery 里的 contains,方法是用来判断dom元素的包含关系的。
$.contains() 方法用于判断指定元素内是否包含另一个元素。即判断另一个DOM元素是否是指定DOM元素的后代。
实例:
$(function () {
function funcontain( html ){
document.body.innerHTML += "<br>" + html;
}
funcontain($.contains( document.documentElement, document.body )); // true
funcontain($.contains( document.body, document.documentElement )); // false
})
but 看文章的时候发现有个数组有个 indexOf 判断数组内某个元素是否存在,只用字符串的 indexOf ,一查文档,发现还真的有,又学到一个。写法和字符串的一致,返回元素所在的位置,没有找到返回-1。
有个大佬的文章讲这个的,分析的不错。
边栏推荐
- King power volume LinkSLA, realize operations engineer is happy fishing
- 618, you may be able to wake up a little bit
- What is Alibaba Cloud Express Beauty Station?
- This is indeed the best article on microservice architecture I have read!
- Configuration of routers and static routes
- Network wiring and digital-to-system conversion
- el-progress实现进度条颜色不同
- Successful indie developers deal with failure & imposters
- js dynamically get screen width and height
- 多线程之传递参数
猜你喜欢

路由器和静态路由的配置

disabledDate 日期选择器 datePicker

GetEnumerator method and MoveNext and Reset methods in Unity

VRRP概述及实验

Mina断线重连

DevOps-了解学习

What?CDN cache acceleration only works for accelerating static content?

Mina的长连接和短连接

Transport layer protocol (TCP 3-way handshake)

King power volume LinkSLA, realize operations engineer is happy fishing
随机推荐
BIO,NIO,AIO实践学习笔记(便于理解理论)
Mina断线重连
vim教程:vimtutor
618, you may be able to wake up a little bit
Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
Native JS takes you to understand the implementation and use of array methods
LeetCode中常用语言的一些基本方法记录
ALC实验
Programmers should understand I/O this way
Operation and maintenance engineer, come and pick up the wool
Browser Storage for H5
Detailed explanation of ten solutions across domains (summary)
网络协议基础-学习笔记
The use of three parameters of ref, out, and Params in Unity3D
From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
js dynamically get screen width and height
What impact does CIPU have on the cloud computing industry?
原生JS带你了解数组方法实现及使用
Advantages of overseas servers
Mina's long and short connections