let that = del; for (let i = that; i > 0; i++) { if (that <= 0) { break; } for (let j = start; j < this.length - 1; j++) { [this[j], this[j + 1]] = [this[j + 1], this[j]]; } arr.push(this[this.length - 1]); this.length--; that--; }
大可不必这样,把删除项的下一位(不删除)往前挪一个一个覆盖要删除的项即可
1 2 3 4 5
for (let i = 0; i < del; i++) { arr.push(this[start + i]); this[start + i] = this[start + i + del]; } this.length -= del;
concat
返回值:拼接后的新数组
思路:如果参数是数组需遍历后一个一个添加到新数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Array.prototype.myConcat = function () { let arr = []; for (let i = 0; i < this.length; i++) { arr[i] = this[i]; } for (let i = 0; i < arguments.length; i++) { const el = arguments[i]; if (Array.isArray(el)) { for (let i = 0; i < el.length; i++) { arr[arr.length] = el[i]; } } else { arr[arr.length] = el; } } return arr; };
Array.prototype.mySort = function (callBack) { if (this.length <= 1) { returnthis; } if (typeof callBack === "function") { for (let i = 0; i < this.length - 1; i++) { for (let j = 0; j < this.length - 1 - i; j++) { if (callBack(this[j], this[j + 1]) > 0) { [this[j], this[j + 1]] = [this[j + 1], this[j]]; } } } } elseif (typeof callBack === "undefined") { for (let i = 0; i < this.length - 1; i++) { for (let j = 0; j < this.length - 1 - i; j++) { if (String(this[j]) > String(this[j + 1])) { [this[j], this[j + 1]] = [this[j + 1], this[j]]; } } } } else { return"参数异常"; } returnthis; };
数组位置方法
以下方法均不改变原数组
indexOf
返回值:索引/-1
思路:遍历数组,第二个参数是从哪个索引开始。如果不传参,从头查到尾部
1 2 3 4 5 6 7 8 9 10 11 12
Array.prototype.myIndexOf = function (val, index = 0) { if (index < 0) { index = -index > this.length ? 0 : index + this.length; } for (let i = index; i < this.length; i++) { if (this[i] === val) { return i; } } return -1; };
lastIndexOf
返回值:索引/-1
思路:遍历数组,第二个参数是到哪个索引结束(从0开始到这个索引结束)。如果不传参,从头查到尾部
1 2 3 4 5 6 7 8 9 10 11
Array.prototype.myLastIndexOf = function (val, index = this.length) { if (index < 0) { index = -index >= this.length ? 0 : index + this.length; } for (let i = index; i >= 0; i--) { if (this[i] === val) { return i; } } return -1; };
includes
返回值:true/false
思路:遍历数组
1 2 3 4 5 6 7 8
Array.prototype.myIncludes = function (val) { for (let i = 0; i < this.length; i++) { if (this[i] === val) { returntrue; } } returnfalse; };
数组迭代方法
以下方法均不改变原数组
forEach
回调函数内部 this 一般指向 window
返回值:undefined
思路:遍历数组
1 2 3 4 5 6
Array.prototype.myForEach = function (callBack) { for (let i = 0; i < this.length; i++) { let index = i, item = this[i]; callBack(item, index); } };
map
返回值:映射后的新数组
思路:遍历数组,把数组每以项经过运算后赋值给新数组
1 2 3 4 5 6 7 8
Array.prototype.myMap = function (callBack) { let arr = []; for (let i = 0; i < this.length; i++) { let index = i, item = this[i]; arr[i] = callBack(item, index); } return arr; };
reduce
返回值:函数累计处理的结果
思路:initial 返回值在数组的每次迭代中被记住,最后成为最终的结果值
1 2 3 4 5 6 7 8 9 10 11 12
Array.prototype.myReduce = function (callBack, initial) { if (typeof callBack !== "function") thrownewTypeError("callBack must be function"); let i = 0; if (typeof initial === "undefined") { initial = this[0]; i = 1; } for (; i < this.length; i++) { initial = callBack(initial, this[i], i) } return initial; };
find
返回值:找到就返回符合的元素,没有返回 undefined
思路:遍历数组
1 2 3 4 5
Array.prototype.myFind = function (callBack) { for (let i = 0; i < this.length; i++) { if (callBack(this[i], i)) returnthis[i] } }
every
返回值:只要有一个不符合返回false,如果都符合返回 true
思路:遍历数组,一假即假
1 2 3 4 5 6
Array.prototype.myEvery = function (callBack) { for (let i = 0; i < this.length; i++) { if (!callBack(this[i], i)) returnfalse } returntrue }
some
返回值:只要有一个符合就返回 true,如果都符合返回 false
思路:遍历数组,一真即真
1 2 3 4 5 6
Array.prototype.mySome = function (callBack) { for (let i = 0; i < this.length; i++) { if (callBack(this[i], i)) returntrue } returnfalse }
filter
返回值:一个新数组,数组里面是符合条件的所有元素
思路:遍历数组
1 2 3 4 5 6 7 8 9 10 11 12
Array.prototype.myFilter = function (callBack) { if (!Array.isArray(this) || !this.length || typeof callback !== 'function') { return [] } let arr = []; for (let i = 0; i < this.length; i++) { if (!callBack(this[i], i)) { arr.push(this[i]); } } return arr }