继续把数组后面迭代的方法用原型写出来……
1、forEach
// forEach(回调函数)
// 功能:就是遍历数组的每一项,并且每一项都要执行一次回调函数的功能.
// 不返回值,所以返回函数默认的undefined.
// 遍历的是数组的快照,如果中途修改或者删除了数组,不会改变遍历的顺序,所以不要修改数组
Array.prototype.myForEach = function (callback) {
const arr = this;
const arg2 = arguments[1] || window;
if (typeof callback !== 'function') {
throw new Error('第一个参数必须是函数');
}
if (arr.length == 0) return;
for (let i = 0; i < arr.length; i++) {
callback.call(arg2, arr[i], i, arr);
}
}2、map
// map(function(item,index){})
// 功能:遍历数组的每一项,每一项都要执行回调函数.
// 返回值:返回一个新数组,数组的长度和原数组一样.
Array.prototype.myMap = function (callback, scope) {
const arr = this;
if (typeof callback !== 'function') {
throw new Error('参数1必须是一个函数');
}
let result = [];
if (arr.length == 0) return result;
scope = scope || window;
for (let i = 0; i < arr.length; i++) {
result.push(callback.call(scope, arr[i], i, arr));
}
return result;
}3、filter
// filter(function(item,index){})
// 功能:对数组的每一项进行筛选,每一项都要执行回调函数,通过函数筛选成功的值,返回到新数组中.
// 返回一个新数组,数组的长度可能比原数组要小.
Array.prototype.myFilter = function (callback, scope) {
const arr = this;
if (typeof callback !== 'function') {
throw new Error('参数1必须是一个函数');
}
let len = arr.length;
let result = [];
if (len == 0) return result;
scope = scope || window;
for (let i = 0; i < len; i++) {
if (callback.call(scope, arr[i], i, arr)) {
result.push(arr[i]);
}
}
return result;
}4、every
// every(function(item,index){})
// 功能:每一项都要执行回调函数,通过了函数的条件,每一项都为true,才返回 true,如果有一项为假,则返回false
// 返回值:布尔值
Array.prototype.myEvery = function (callback, scope) {
const arr = this;
if (typeof callback !== 'function') {
throw new Error('参数1必须是一个函数');
}
let len = arr.length;
if (len == 0) return true;
scope = scope || window;
for (let i = 0; i < len; i++) {
// 如果有一个不满足,返回false
if (!callback.call(scope, arr[i], i, arr)) {
return false;
}
}
return true;
}5、some
// some(function(item,index){})
// 功能:数组的某一项通过了函数的条件判断,则返回true,如果都没有通过,才返回false
// 返回值:布尔值
Array.prototype.mySome = function (callback, scope) {
const arr = this;
if (typeof callback !== 'function') {
throw new Error('参数1必须是一个函数');
}
let len = arr.length;
if (len == 0) return false;
scope = scope || window;
for (let i = 0; i < len; i++) {
// 如果有一个满足,返回true
if (callback.call(scope, arr[i], i, arr)) {
return true;
}
}
return false;
}6、reduce
// reduce(function(sum,i){},0)
// 功能:累计
Array.prototype.myReduce = function (callback, init) {
const arr = this;
if (typeof callback !== 'function') {
throw new Error('参数1必须是一个函数');
}
let len = arr.length;
// 如果长度为0,又没有设置初始值,报错
if (len == 0 && init === undefined) {
throw new Error('数组为空时必须设置初始值');
}
// 如果长度为0,返回初始值
if (len == 0) return init;
// 总的值默认为初始值,没有初始值,默认为0
let total = init || 0;
// 没有设置初始值的时候,total默认为第一个数组的值,选项从第二个开始遍历。
if (init === undefined) {
total = arr[0];
for (let i = 1; i < len; i++) {
total = callback(total, arr[i], i, arr)
}
} else {
// 设置了初始值,则从第一个项开始遍历
for (let i = 0; i < len; i++) {
total = callback(total, arr[i], i, arr)
}
}
return total;
}还有includes、find、findIndex等等新增的方法就不写了,差别不大,好吧,其实是我想偷懒了,累觉不爱……
发表评论:
◎请发表你卖萌撒娇或一针见血的评论,严禁小广告。