1.map方法
Array.prototype.myMap = function(callback) {
let result = [];
for(let i = 0; i < this.length; i++) {
result.push(callback(this[i], i, this));
}
return result;
}
// 测试用例
const arr = [1, 2, 3];
const mappedArr = arr.myMap((item) => item * 2);
console.log(mappedArr); // [2, 4, 6]
2.filter方法
Array.prototype.myFilter = function(callback) {
let filteredArr = [];
for(let i = 0; i < this.length; i++) {
if(callback(this[i], i, this)) {
filteredArr.push(this[i]);
}
}
return filteredArr;
}
// 测试用例
const arr = [1, 2, 3, 4, 5];
const filteredArr = arr.myFilter(num => num > 2);
console.log(filteredArr); // [3, 4, 5]
3.some方法
Array.prototype.mySome = function(callback) {
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
return true;
}
}
return false;
};
// 测试用例
const numbers = [1, 2, 3, 4, 5];
// 测试1: 检查是否存在偶数
const hasEvenNumber = numbers.mySome(function(element) {
return element % 2 === 0;
});
console.log(hasEvenNumber); // 输出: true
4.every方法
Array.prototype.myEvery = function(callback) {
for (let i = 0; i < this.length; i++) {
if (!callback(this[i], i, this)) {
return false;
}
}
return true;
};
// 测试用例const numbers = [1, 2, 3, 4, 5];
// 测试1: 检查是否所有元素都是正数
const allPositiveNumbers = numbers.myEvery(function(element) {
return element > 0;
});
console.log(allPositiveNumbers); // 输出: true
5.reduce方法
Array.prototype.myReduce = function(callback, initialValue) {
let accumulator = initialValue !== undefined ? initialValue : this[0];
const startIndex = initialValue !== undefined ? 0 : 1;
for (let i = startIndex; i < this.length; i++) {
accumulator = callback(accumulator, this[i], i, this);
}
return accumulator;
};
// 测试用例
const numbers = [1, 2, 3, 4, 5];
// 测试1: 计算所有元素的和
const sum = numbers.myReduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
console.log(sum); // 输出: 15
6.find方法
Array.prototype.find = function(callback) {
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
return this[i];
}
}
return undefined;
};
// 测试用例
const numbers = [1, 2, 3, 4, 5];
const foundNumber = numbers.find(function(num) {
return num > 3;
});
console.log(foundNumber); // 输出: 4
7.flat方法
//扁平化
function flat(arr) {
const result = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
result.push(...flat(arr[i]));
} else {
result.push(arr[i]);
}
}
return result;
}
// 测试用例
const arr1 = [1, 2, [3, 4], 5];
console.log(flat(arr1)); // [1, 2, 3, 4, 5]
8.entries方法
function entries(arr) {
const result = [];
for (let i = 0; i < arr.length; i++) {
result.push([i, arr[i]]);
}
return result;
}
// 测试用例
const arr1 = ['a', 'b', 'c'];
console.log(entries(arr1)); // [[0, 'a'], [1, 'b'], [2, 'c']]
9.forEach方法
Array.prototype.myForEach = function(callback) {
for (let i = 0; i < this.length; i++) {
callback(this[i], i, this);
}
};
// 测试用例
const arr = [1, 2, 3];
arr.myForEach((item, index, array) => {
console.log(`item: ${item}, index: ${index}, array: ${array}`);
});
// 输出:
// item: 1, index: 0, array: 1,2,3
// item: 2, index: 1, array: 1,2,3
// item: 3, index: 2, array: 1,2,3
10.isArray方法
Array.prototype.myIsArray = function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
// 测试用例
console.log(Array.myIsArray([])); // true
console.log(Array.myIsArray({})); // false
console.log(Array.myIsArray('')); // false
console.log(Array.myIsArray(123)); // false
11.sort方法
Array.prototype.mySort = function(compareFunc) {
for (let i = 0; i < this.length - 1; i++) {
for (let j = i + 1; j < this.length; j++) {
if (compareFunc ? compareFunc(this[i], this[j]) > 0 : this[i] > this[j]) {
[this[i], this[j]] = [this[j], this[i]];
}
}
}
return this;
};
// 测试用例
const arr1 = [3, 2, 1];
console.log(arr1.mySort()); // [1, 2, 3]
const arr2 = ['c', 'b', 'a'];
console.log(arr2.mySort()); // ['a', 'b', 'c']
const arr3 = [{name: 'Tom', age: 18}, {name: 'Jerry', age: 20}, {name: 'Bob', age: 15}];
console.log(arr3.mySort((a, b) => a.age - b.age)); // [{name: 'Bob', age: 15}, {name: 'Tom', age: 18}, {name: 'Jerry', age: 20}]
12.from方法
Array.myFrom = function(obj, mapFunc, thisArg) {
const result = [];
const len = obj.length;
for (let i = 0; i < len; i++) {
if (mapFunc) {
result.push(mapFunc.call(thisArg, obj[i], i, obj));
} else {
result.push(obj[i]);
}
}
return result;
};
// 测试用例
console.log(Array.myFrom('hello')); // ['h', 'e', 'l', 'l', 'o']
console.log(Array.myFrom([1, 2, 3], x => x * 2)); // [2, 4, 6]
console.log(Array.myFrom({length: 3, 0: 'a', 1: 'b', 2: 'c'})); // ['a', 'b', 'c']
最后欢迎大家关注微信公众号: 前端就业课,带大家上高速