手写reduce
// 手写reduce
Array.prototype.myReduce = function (fn, acc) {
if (typeof fn !== "function") {
throw "fn必须是个函数";
}
if (!Array.isArray(this)) {
throw "调用者必须是个数组";
}
let index = 0;
if (!acc) {
acc = this[0];
index = 1;
}
for (index; index < this.length; index++) {
acc = fn(acc, this[index], index, this);
}
return acc;
};
// 定义调用
let arr = [1, 2, 3, 4, 5];
let result = arr.myReduce((acc, val) => {
return acc + val;
}, 0);
手写深克隆
let deepClone = function (object) {
if (!object || typeof object !== "object") return;
let newObj = Array.isArray(object) ? [] : {};
for (const key in object) {
if (Object.hasOwnProperty.call(object, key)) {
newObj[key] =
typeof object[key] == "object"
? deepClone(object[key])
: object[key];
}
}
return newObj;
};
let data = {
name: "张三",
info: {
sex: "男",
age: 20,
},
};
let cloneData = deepClone(data);
console.log(cloneData);
节流和防抖
// 节流
function throttle(fn, delay) {
let timer = null;
return () => {
if (!timer) {
timer = setTimeout(() => {
fn();
timer = null;
}, delay);
}
};
}
// 使用示例
function myFunction() {
console.log("Function is called");
}
const throttledFunction = throttle(myFunction, 1000);
// 防抖
function debounce(fn, dealy) {
let timer = null;
return () => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
fn();
timer = null;
}, dealy);
};
}
const debounceFunction = debounce(myFunction, 1000);
this.$refs.button.addEventListener("click", () => {
//节流测试
// throttledFunction()
// 防抖测试
debounceFunction();
});
数组扁平化
Array.prototype.myFlat = function (deep) {
//第一种
// let newArr=[]
// console.log(...this);
// newArr=newArr.concat(...this)
// 第二种
// let newArr=[]
// for (let index = 0; index < this.length; index++) {
// newArr=newArr.concat(Array.isArray(this[index])?this[index].myFlat():this[index])
// }
// 第三种,最终版本
let newArr = [];
for (let index = 0; index < this.length; index++) {
const ele = this[index];
if (Array.isArray(ele) && deep > 0) {
newArr = newArr.concat(ele.myFlat(deep - 1));
} else {
newArr.push(ele);
}
}
return newArr;
};
let arr1 = [1, 2, [4, 5, [7, 8, 9], 6], 3];
let flatArr = arr1.myFlat(1);
console.log(flatArr);
排序
// 排序
Array.prototype.mySort = function () {
for (let i = 0; i < this.length; i++) {
for (let j = 0; j < this.length - i - 1; j++) {
if (this[j] > this[j + 1]) {
[this[j], this[j + 1]] = [this[j + 1], this[j]];
}
}
}
return this;
};
let arr2 = [1, 3, 5, 2, 4];
let sortArr = arr2.mySort();
console.log(sortArr);
function quickSort(arr) {
//必须加限制,不然会无法停止
if (arr.length <= 1) {
return arr;
}
let middleIndex = Math.floor(arr.length / 2);
let middleValue = arr[middleIndex];
let left = [];
let right = [];
// console.log(arr);
for (let index = 0; index < arr.length; index++) {
const ele = arr[index];
if (ele > middleValue) {
right.push(ele);
}
if (ele < middleValue) {
left.push(ele);
}
}
return [...quickSort(left),middleValue,...quickSort(right)]
}
console.log(quickSort(arr2));
手写filter
// 手写filter
Array.prototype.myFilter = function (fn) {
if (typeof fn !== "function") {
throw "fn必须是个函数";
}
let res = [];
for (let index = 0; index < this.length; index++) {
if (fn(this[index])) {
res.push(this[index]);
}
}
return res;
};
let arr3 = [1, 2, 3, 4, 5, 6];
let filterArr=arr3.myFilter((item) => {
return item > 3;
});
console.log(filterArr);