-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy patharrary-function.js
147 lines (122 loc) · 3.7 KB
/
arrary-function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* eslint-disable no-extend-native */
/*
* Author : OBKoro1
* Date : 2021-08-06 02:04:50
* LastEditors : OBKoro1
* LastEditTime : 2023-02-07 23:42:11
* FilePath : /web-basics/src/js/arrayFunction/arrary-function.js
* description : 数组方法forEach、filter map reduce some every 实现原理
* koroFileheader VSCode插件
* Copyright (c) 2021 by OBKoro1, All Rights Reserved.
*/
Array.prototype.mySplice = function (start, deleteCount, ...addList) {
if (start < 0) {
if (Math.abs(start) > this.length) {
start = 0
} else {
start += this.length
}
}
if (typeof deleteCount === 'undefined') {
deleteCount = this.length - start
}
// 删除
const removeList = this.slice(start, start + deleteCount)
const right = this.slice(start + deleteCount)
// 添加
let addIndex = start
addList.concat(right).forEach((item) => {
this[addIndex] = item
addIndex++
})
this.length = addIndex
return removeList
}
// forEach实现
Array.prototype.myForEach = function (fn, thisArgs) {
if (typeof fn !== 'function') throw 'Error in params'
const len = this.length // 在遍历的开始就确定遍历次数 对元素增删改查不会影响遍历次数
// 遍历使用回调 传递参数
for (let i = 0; i < len; i++) {
fn.call(thisArgs, this[i], i, this)
}
}
// 使用
const oldArr = [1, 2, 3, 4, 5]
oldArr.myForEach((item, index, arr) => {
console.log('myForEach', item, index, arr)
})
// filter实现
Array.prototype.myFilter = function (callBack) {
const newArr = []
// 1. 循环
for (let i = 0; i < this.length; i++) {
const item = this[i]
// 2. 执行回调 满足条件 添加它
if (callBack(item, i)) {
newArr.push(item)
}
}
return newArr // 3. 返回新数组
}
// 使用
const oldFilterArr = [1, 2, 3, 4, 5]
const filterNewArr = oldFilterArr.myFilter((item) => item <= 3) // 过滤3以及3以下
console.log('filter 重写', filterNewArr)
// map实现
Array.prototype.myMap = function (callBack) {
const newArr = []
// 1. 循环
for (let i = 0; i < this.length; i++) {
// 2. 执行回调 添加回调的返回值
const newItem = callBack(this[i], i)
newArr.push(newItem)
}
return newArr // 3. 返回新数组
}
const oldMapArr = [1, 2, 3, 4, 5]
const newMapArr = oldMapArr.myMap((item) => `${item}元素处理`)
console.log('map 重写', newMapArr)
// reduce 实现原理
Array.prototype.myReduce = function (callBack, pre) {
// 1. 循环
for (let i = 0; i < this.length; i++) {
if (pre !== undefined) {
// 2. 传入已有的pre 与 当前循环值 赋值到pre上
pre = callBack(pre, this[i], i, this)
} else {
// 3. 如果没传入pre 将数组当前项当做pre传入 并增加指针
pre = callBack(this[i], this[i + 1], i, this)
i++
}
}
return pre // 4. 返回pre
}
const oldReduce = [1, 2, 3, 4, 5]
const reduceRes = oldReduce.myReduce((prev, curr, index) => prev + curr, 0)
console.log('reduce 重写', reduceRes)
// some 实现原理
Array.prototype.mySome = function (callBack) {
for (let i = 0; i < this.length; i++) {
if (callBack(this[i])) {
return true // 有一个元素符合要求 即成
}
}
return false
}
const oldSome = [1, 2, 3, 4, 5]
// 如果有一个值大于4 则返回true
const someIsTrue = oldSome.mySome((item) => item > 4)
console.log('some 重写', someIsTrue)
// every 实现原理
Array.prototype.myEvery = function (callBack) {
for (let i = 0; i < this.length; i++) {
if (!callBack(this[i])) {
return false // 有一个元素错误即失败
}
}
return true
}
const everyArr = [1, 2, 3, 4, 5]
const everyIsTrue = everyArr.myEvery((item) => item > 0)
console.log('every 重写', everyIsTrue)