当前位置:网站首页>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。
有个大佬的文章讲这个的,分析的不错。
边栏推荐
- Transformer详细解读与预测实例记录
- 云计算基础-学习笔记
- 浏览器存储WebStorage
- 请问下通过flink sql读取hologres 的两张表的 binlog,然后如何进行join?
- Unity realizes first-person roaming (nanny-level tutorial)
- 初识网页与浏览器
- 系统基础-学习笔记(一些命令记录)
- Xiaodu Xiaodu is here!
- Will intelligent operation and maintenance replace manual operation and maintenance?
- Next-Generation Parsing Technology - Cloud Parsing
猜你喜欢
Into the pre-service, thought they play so flowers
What?CDN cache acceleration only works for accelerating static content?
增长:IT运维发展趋势报告
Q 2020, the latest senior interview Laya soul, do you know?
el-progress实现进度条颜色不同
DevOps流程demo(实操记录)
Problems encountered in installing Yolo3 target detection module in Autoware
selenium学习
I/O performance and reliability
DevOps - Understanding Learning
随机推荐
The highlight moment of operation and maintenance starts with intelligence
[ingress]-ingress exposes services using tcp port
From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
浏览器存储WebStorage
[ingress]-ingress使用tcp端口暴露服务
Switch principle
传输层协议
flink cdc 目前支持Gauss数据库源吗
Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
RAID disk array
程序员应该这样理解I/O
Mongodb query analyzer parsing
BIO,NIO,AIO实践学习笔记(便于理解理论)
大小屏适配
Will intelligent operation and maintenance replace manual operation and maintenance?
实力卷王LinkSLA,实现运维工程师快乐摸鱼
初识网页与浏览器
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced
Proprietary host CDH
sql server 重复值在后面计数