这个是兼容node和浏览器环境的,不需要可以删掉
try { _this = _this || window; } catch (e) { _this = {}; }复制代码
reverse
function myReverse(arr) { var times = (arr.length / 2) | 0; for (var i = 0; i < times; i++) { [arr[i], arr[arr.length - 1 - i]] = [arr[arr.length - 1 - i], arr[i]] }}复制代码
forEach
Array.prototype.myForEach = function (fn, _this) { var length = this.length; //兼容node 和 chrome try { _this = _this || window; } catch (e) { _this = {}; } if (fn) { for (var i = 0; i < length; i++) { fn.apply(_this, [this[i], i, this]); } }}复制代码
filter
Array.prototype.myFilter = function (fn, _this) { var arr = []; var length = this.length; try { _this = _this || window; } catch (e) { _this = {}; } if (fn) { for (var i = 0; i < length; i++) { fn.apply(_this, [this[i], i, this]) && arr.push(this[i]) } } return arr;}var arr = [1, 2, 3, 4, 5];var newArr = arr.myFilter((ele, index, self) => { return ele > 3 ? true : false})console.log(newArr)复制代码
map
Array.prototype.myFilter = function (fn, _this) { var arr = []; var length = this.length; try { _this = _this || window; } catch (e) { _this = {}; } if (fn) { for (var i = 0; i < length; i++) { fn.apply(_this, [this[i], i, this]) && arr.push(this[i]) } } return arr;}var arr = [1, 2, 3, 4, 5];var newArr = arr.myFilter((ele, index, self) => { return ele > 3 ? true : false})console.log(newArr)复制代码
every
判断是否数组中的每一个元素都符合条件, 如果都符合条件,返回true, 如果不符合,返回false
Array.prototype.every = function (fn, _this) { var length = this.length; try { _this = _this || window; } catch (e) { _this = {}; } if (fn) { for (var i = 0; i < length; i++) { var returnValue = fn.apply(_this, [this[i], i, this]); if (!returnValue) { return false; } } } return true;}var arr = [2, 3, 45, 6, 7, 8, 9];var flag = arr.every((ele, index, self) => { return ele < 100})console.log(flag)复制代码
some
数组中是否有一个符合条件, 如果有的话, 返回true, 否则返回false
Array.prototype.mySome = function (fn, _this) { var length = this.length; try { _this = _this || window; } catch (e) { _this = {}; } if (fn) { for (var i = 0; i < length; i++) { var returnValue = fn.apply(_this, [this[i], i, this]); if (returnValue) { return true; } } } return false;}var arr = [2, 3, 45, 6, 7, 8, 9];var flag = arr.mySome((ele, index, self) => { return ele < 3})console.log(flag)复制代码
reduce 接力传递
使用时要有返回值
Array.prototype.myReduce = function (fn, prevValue, _this) { var length = this.length; try { _this = _this || window; } catch (e) { _this = {}; } if (prevValue) { for (var i = 0; i < length; i++) { prevValue = fn.apply(_this, [prevValue, this[i], i, this]); } } else if (!prevValue) { prevValue = this[0]; for (var i = 1; i < length; i++) { prevValue = fn.apply(_this, [prevValue, this[i], i, this]); } } return prevValue;}var arr = [7, 8, 9];var sum = arr.myReduce((prevValue, currentValue, index, self) => { return prevValue += currentValue}, 6)console.log(sum);复制代码
reduceRight 反向接力传递
Array.prototype.myReduceRight = function (fn, prevValue, _this) { var length = this.length; try { _this = _this || window; } catch (e) { _this = {}; } if (prevValue) { for (var i = length - 1; i >= 0; i--) { prevValue = fn.apply(_this, [prevValue, this[i], i, this]); } } else if (!prevValue) { prevValue = this[length - 1]; for (var i = length - 2; i >= 0; i--) { prevValue = fn.apply(_this, [prevValue, this[i], i, this]); } } return prevValue;}var arr = [1,2,3];var sum = arr.myReduceRight((prevValue, currentValue, index, self) => { return prevValue += currentValue}, 5)console.log(sum)复制代码
Array上面的全部方法:
//连接数组, 返回值是新的连接好的数组, 不会改变原数组concat: ƒ concat() //填充相同的元素fill: ƒ fill()//查找到最近的符合条件的find: ƒ find()//查找到最近的符合条件的元素的索引findIndex: ƒ findIndex()[3,2].findIndex((ele)=>ele>1) // 1//浅层扁平化flat: ƒ flat()[1,[2,[3]]].flat() // [1, 2, Array(1)]// 是否含有includes: ƒ includes()// 从后向前查找lastIndexOf: ƒ lastIndexOf()//截取并返回新数组,不会改变原数组slice: ƒ slice()// 改变原数组, 实现增删,返回被截取掉的部分splice: ƒ splice()复制代码