首页 技术 正文
技术 2022年11月17日
0 收藏 701 点赞 3,679 浏览 2728 个字

原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter

Array 中的高阶函数 —- map, filter, reduce


map() – 映射

var newArr = array.map((currentValue, index, array) => { return ... }, thisValue);
  • currentValue, 必须,当前的元素值;
  • index, 可选,当前元素值的索引;
  • array, 可选,原数组;
  • thisValue, 可选,对象作为该执行回调时使用,传递给函数,用作 “this” 的值;
  • return 新数组;

栗子:

var array1 = [1,4,9,16];
const map1 = array1.map(x => x *2);console.log(array1); // [1,4,9,16]
console.log(map1); // [2,8,18,32]

注意:

  1. map() 不会对空数组进行检测;

filter() – 过滤,筛选

var newArr = array.filter((currentValue, index, array) => { return ... }, thisValue);
  • currentValue, 必须,当前的元素值;
  • index, 可选,当前元素值的索引;
  • array, 可选,原数组;
  • thisValue, 可选,对象作为该执行回调时使用,传递给函数,用作 “this” 的值;
  • return 新数组;

栗子:过滤不符合项

var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40) console.log(arr) // [20,30,50, 96,50]
console.log(newArr) // [50, 96, 50]

高频用途:

  1. 上例中的过滤不符合项;
  2. 去掉数组中的 空字符串、0、undefined、null;
    var arr = ['1', '2', null, '3.jpg', null, 0]
    var newArr = arr.filter(item => item)
    // 也可以写成
    // var newArr = arr.filter(Boolean);
    console.log(newArr) // ["1", "2", "3.jpg"]
  3. 数组去重;

注意:

  1. filter() 不会对空数组进行检测;

reduce – 累计

var result = array.reduce((total, currentValue, currentIndex, array) => { return ... }, initialValue);
  • total, 必须,初始值,第一次循环之后是计算后的返回值;
  • currentValue, 必须,当前的元素值;
  • currentIndex, 可选,当前元素值的索引;
  • array, 可选,原数组;
  • initialValue, 可选,传递给函数的初始值,即此值会在第一次循环之前赋值给 total;
  • return 经过处理过的 total;

栗子:统计字符串中每个字符出现的次数

const str = '9kFZTQLbUWOjurz9IKRdeg28rYxULHWDUrIHxCY6tnHleoJ'
const obj = {}
Array.from(str).reduce((accumulator, current) => {
current in accumulator ? accumulator[current]++ : accumulator[current] = 1
return accumulator;
}, obj)

当然,非 reduce 的写法是:

const str = '9kFZTQLbUWOjurz9IKRdeg28rYxULHWDUrIHxCY6tnHleoJ'
const obj = {}
str.split('').forEach(item => {
obj[item] ? obj[item]++ : obj[item] = 1
})

reduce 的用途很广泛,可以说,js 中有关数组循环的模块都可以使用 reduce 来实现,这里不一一列举,详见 reduce-MDN

js 实现 map


原生 js 实现:

Array.prototype.myMap = function(fn, context = window) {
if (typeof fn !== 'function') return;
let newArr = [];
for(let i = 0, len = this.length; i < len; i++) {
newArr.push(fn.call(context, this[i], i, this))
}
return newArr;
}// 使用
[1,2,3].myMap(function(v, i, arr) {
console.log(v, i, arr);
return v * 2;
})

有一点奇怪的是,需要先在 Array 上挂 myMap() 这个方法,然后回车后才能使用,如果将上述代码全部复制进浏览器控制台,回车运行会报错,这是为什么?

使用 reduce 实现:

Array.prototype.reduceMap = function(fn, context = window) {
if (typeof fn !== 'function') return;
// or if (typeof fn !== 'function') throw new TypeError(fn + 'is not a function') ;
let _this = this;
let newArr = _this.reduce(function(total, cV, cI, _this) {
return total.concat([fn.call(context, cV, cI, _this)])
}, [])
return newArr
}

上面的示例是挂载在 Array 上的,下面这个示例是函数式编程示例:

let fpReduceMap = (fn, context = window) => {
return targetArr => {
if (typeof fn !== 'function') throw new TypeError(fn + 'is not a function')
if (!Array.isArray(targetArr)) throw new TypeError('targetArr must be a Array')
if (targetArr.length == 0) return [];
return targetArr.reduce((total, cV, cI, targetArr) => {
return total.concat([fn.call(context, cV, cI, targetArr)])
}, [])
}
}
// 使用
fpReduceMap(function(v) {
console.log(this);
return v + 1;
}, {msg: 'mapping'})([1,2,3])
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905