- 数组扁平化
const arr = [1, [2, [3, [4, 5]]], 6];
const res = arr.flat(Infinity);
console.log(res);
function flatDeep(arr, d = 1) {
return d > 0
? arr.reduce(
(acc, val) =>
acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val),
[]
)
: arr.slice();
}
const res2 = flatDeep(arr, Infinity);
console.log(res2);
function flatten(arr) {
return arr.reduce((acc, curr) => {
return acc.concat(Array.isArray(curr) ? flatten(curr) : curr);
}, []);
}
console.log(flatten(arr));
const res3 = JSON.stringify(arr).replace(/\[|\]/g, "").split(",");
console.log(res3);
const str = "[" + JSON.stringify(arr).replace(/\[|\]/g, "") + "]";
console.log(JSON.parse(str));
const res4 = [];
const recursivity = (arr) => {
for (let item of arr) {
if (Array.isArray(item)) {
recursivity(item);
} else {
res4.push(item);
}
}
};
recursivity(arr);
console.log(res4);
- 数组去重
const arr = [1, 1, "1", 17, true, true, false, false, "true", "a", {}, {}];
const res1 = [...new Set(arr)];
console.log("res1:", res1);
function unique(arr) {
let len = arr.length - 1;
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1);
j--;
len--;
}
}
}
return arr;
}
console.log("res2:", unique(arr));
const unique2 = (arr) => {
const res = [];
for (let i = 0; i < arr.length; i++) {
if (!res.includes(arr[i])) {
res.push(arr[i]);
}
}
return res;
};
console.log(
"res3:",
unique2([1, 1, "1", 17, true, true, false, false, "true", "a", {}, {}])
);
const res4 = arr.filter((curr, idx) => {
return arr.indexOf(curr) === idx;
});
console.log("res4", res4);
const map = new Map();
const res5 = [];
for (let item of arr) {
if (!map.has(item)) {
res5.push(item);
map.set(item, true);
}
}
console.log("res5:", res5);
- 类数组转化为数组
arr.from(document.querySelectorAll("div"));
[...document.querySelectorAll("div")];
Array.prototype.slice.call(document.querySelectorAll("div"));
Array.prototype.concat.apply([], document.querySelectorAll("div"));
Array.prototype.filter()
const arr = [1, 1, "1", 17, true, true, false, false, "true", "a", {}, {}];
arr.filter((curr, idx, arr) => {}, null);
Array.prototype.myfilter = function (callback, thisArg) {
const res = [];
for (let i = 0; i < this.length; i++) {
callback && callback.call(thisArg, this[i], i, this) && res.push(this[i]);
}
return res;
};
Array.prototype.map()
const arr = [1, 1, "1", 17, true, true, false, false, "true", "a", {}, {}];
arr.map((curr, idx, arr) => {
return curr + 1;
}, null);
Array.prototype.mymap = function (callback, thisArg) {
const res = [];
for (let i = 0; i < this.length; i++) {
res[i] = callback && callback.call(thisArg, this[i], i, this);
}
return res;
};
Array.prototype.forEach()
const arr = [1, 1, "1", 17, true, true, false, false, "true", "a", {}, {}];
arr.forEach((curr, idx, arr) => {
}, null);
Array.prototype.myforEach = function (callback, thisArg) {
const res = [];
for (let i = 0; i < this.length; i++) {
callback && callback.call(thisArg, this[i], i, this);
}
return res;
};
Array.prototype.every()
const arr = [1, 1, "1", 17, true, true, false, false, "true", "a", {}, {}];
arr.every((curr, idx, arr) => {
}, null);
Array.prototype.myevery = function (callback, thisArg) {
for (let i = 0; i < this.length; i++) {
if (callback && !callback.call(thisArg, this[i], i, this)) {
return false;
}
}
return true;
};
Array.prototype.some()
const arr = [1, 1, "1", 17, true, true, false, false, "true", "a", {}, {}];
arr.some((curr, idx, arr) => {
}, null);
Array.prototype.mysome = function (callback, thisArg) {
for (let i = 0; i < this.length; i++) {
if (callback && callback.call(thisArg, this[i], i, this)) {
return true;
}
}
return false;
};
Array.prototype.reduce()
const arr = [1, 1, "1", 17, true, true, false, false, "true", "a", {}, {}];
arr.reduce((acc, curr, idx, arr) => {
return acc;
}, []);
Array.prototype.myreduce = function (callback, initial) {
let acc;
let start = 0;
if (initial) {
acc = initial;
} else {
acc = this[0];
start = 1;
}
for (let i = start; i < this.length; i++) {
callback && callback(acc, this[i], i, this);
}
return acc;
};
Array.prototype.join
Array.prototype.myjoin = function (str = ",") {
let resStr = "";
for (let i = 0; i < this.length; i++) {
let item = this[i];
resStr = i === 0 ? item : `${resStr}${str}${item}`;
}
return resStr;
};
Array.prototype.flat
Array.prototype.myflat = function (deep = Infinity) {
let arr = this;
let i = 0;
while (arr.some((item) => Array.isArray(item))) {
arr = [].concat(...arr);
i++;
if (i >= deep) {
break;
}
}
return arr;
};
Array.prototype.splice
Array.prototype.mysplice = function (start, len, ...values) {
if (len === 0) {
return [];
}
len = start + len > this.length - 1 ? this.length - start : this.length;
};
Array.prototype.apply
Array.prototype.myapply = function (thisArg, argsArr) {
if (typeof this !== "function") {
throw Error("no function");
}
if (thisArg === null || thisArg === undefined) {
thisArg = window;
} else {
thisArg = Object(thisArg);
}
const func = new Symbol("func");
thisArg[func] = this;
const result = thisArg[func](...argsArr);
delete thisArg[func];
return result;
};
function foo(...args) {
return args;
}
foo.myapply(null, [1, 2]);
Array.prototype.call
Array.prototype.mycall = function (thisArg, ...argsArr) {
if (typeof this !== "function") {
throw Error("no function");
}
if (thisArg === null || thisArg === undefined) {
thisArg = window;
} else {
thisArg = Object(thisArg);
}
const func = new Symbol("func");
thisArg[func] = this;
const result = thisArg[func](...argsArr);
delete thisArg[func];
return result;
};
function foo(...args) {
return args;
}
foo.myapply(null, [1, 2]);
Array.prototype.bind
Array.prototype.mybind = function (thisArg, ...argsArr) {
if (typeof this !== "function") {
throw Error("no function");
}
if (thisArg === undefined || thisArg === null) {
thisArg = window;
} else {
thisArg = Object(thisArg);
}
let func = this;
return function (...args) {
return func.apply(this instanceof func ? this : thisArg, args);
};
};
function foo(...args) {
return args;
}
const bar = foo.mybind(null, [1, 2]);
bar(1, 2);
debounce
function debounce(func, wait) {
let timer = null;
return function () {
clearTimeout(timer);
timer = setTimeout(() => func(), wait);
};
}
function debounce2(func, wait, immediate) {
let timer = null;
return function () {
clearTimeout(timer);
if (immediate) {
let callNow = !timer;
timer = setTimeout(() => func(), wait);
if (callNow) {
func();
}
} else {
timer = setTimeout(() => func(), wait);
}
};
}
function foo(...args) {
console.log(1);
return args;
}
const bar = debounce(foo, 3000);
const bar1 = debounce2(foo, 3000, true);
for (let i = 0; i < 30000; i++) {
bar1();
}
throttle
function throttle1(func, wait) {
let start = +new Date();
return function () {
let end = +new Date();
if (end - start >= wait) {
func();
start = +new Date();
}
};
}
function throttle2(func, wait) {
let start = +new Date();
let timer = null;
return function () {
let end = +new Date();
let remaining = wait - (end - start);
clearTimeout(timer);
if (remaining <= 0) {
func();
start = +new Date();
} else {
timer = setTimeout(() => func(), remaining);
}
};
}
function foo() {
console.log("1");
}
const bar = throttle2(foo, 3000);
for (let i = 0; i < 300000000000; i++) {
bar();
}
- 函数柯里化
function add(...argsArr) {
const _argsArr = [...argsArr];
function func(...args) {
_argsArr.push(...args);
return func;
}
func.console = function () {
console.log(_argsArr);
};
func.sum = function () {
return _argsArr.reduce((sum, curr) => {
return (sum += curr);
}, 0);
};
func.toString = function () {
return _argsArr.reduce((sum, curr) => {
return (sum += curr);
}, 0);
};
return func;
}
const res1 = add(1)(2)(3)(4);
const res2 = add(1, 2, 4)(2)(3)(4);
console.log(res1.console());
console.log(res2.console());
console.log(res1.sum());
console.log(res2.sum());
console.log(res1.toString());
console.log(res2.toString());
new
function mynew(F, ...args) {
let obj = {};
Object.setPrototypeOf(obj, F.prototype);
const res = F.apply(obj, args);
const isObject = typeof res === "object" && typeof res !== "null";
const isFunction = typeof res === "function";
return isObject || isFunction ? res : obj;
}
function Foo(...args) {
this.args = args;
}
const res = mynew(Foo, 1, 2, 3);
console.log(res);
instanceof
function myinstanceof(left, right) {
if (
(typeof left !== "object" && typeof left !== "function") ||
typeof left === null
) {
return false;
}
let proto = Object.getPrototypeOf(left);
while (true) {
if (proto === null) {
return false;
}
if (proto === right.prototype) {
return true;
}
proto = Object.getPrototypeOf(proto);
}
}
function foo() {}
const res = myinstanceof(foo, Function);
console.log(res);
- 寄生组合式继承
function Parent(...args) {
this.args = args;
this.getArgs = function () {
console.log(this.args);
};
}
Parent.prototype.method = function () {
console.log("perent method");
};
function Child(...args) {
Parent.call(this, ...args);
this.childArgs = args;
}
function _extends(Child, Parent) {
let proto = Object.create(Parent.prototype);
Child.prototype = proto;
Child.prototype.constructor = Child;
}
_extends(Child, Parent);
const child = new Child(1, 2);
console.log(child);
console.log(child.__proto__.__proto__);
console.log(Object.getPrototypeOf(child));
child.method();
Object.is
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
console.log(is(0, -0));
console.log(is(NaN, NaN));
Object.assign
Object.defineProperty(Object, "myassign", {
value: function (target, ...args) {
if (target === null) {
throw Error("not null");
}
const to = Object(target);
for (let source of args) {
if (typeof source !== null) {
for (let key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
to[key] = source[key];
}
}
}
}
return to;
},
enumerable: false,
writable: false,
configurable: true,
});
const res = Object.myassign({}, { foo: 1, bar: "2" });
console.log(res);
- 深、浅拷贝
const shallowClone = (obj) => {
const newObj = {};
for (let key of obj) {
if (Object.hasOwnProperty.call(obj, key)) {
newObj[key] = obj[key];
}
}
return newObj;
};
function deepClone(obj, has = new WeakMap()) {
if (obj === null || obj === undefined) {
return obj;
}
if (obj instanceof Date) {
return new Date(obj);
}
if (obj instanceof RegExp) {
return new RegExp(obj);
}
if (obj !== "object") {
return obj;
}
if (has.get(obj)) {
return has.get(obj);
}
let cloneObj = new obj.constructor();
hash.set(obj, cloneObj);
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
cloneObj[key] = deepClone(obj[key], hash);
}
}
return cloneObj;
}
Promise
const PENDING = "PENDING";
const FULFILLED = "FULFILLED";
const REJECTED = "REJECTED";
class Promise {
constructor(excutor) {
this.status = PENDING;
this.value = null;
this.reason = null;
this.onFullfilledCallbacks = [];
this.onRejectedCallbacks = [];
const resovle = (value) => {
if (this.status === PENDING) {
this.value = value;
this.status = FULFILLED;
this.onFullfilledCallbacks.forEach((cb) => cb());
}
};
const reject = (reason) => {
if (this.status === PENDING) {
this.status = REJECTED;
this.reason = reason;
this.onRejectedCallbacks.forEach((cb) => cb());
}
};
try {
excutor(resovle, reject);
} catch (e) {
reject(e);
}
}
then(onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled === "function" ? onFulfilled : (v) => v;
onRejected = typeof onRejected === "function" ? onRejected : (r) => r;
const self = this;
return new Promise((resolve, reject) => {
if (self.status === PENDING) {
self.onFullfilledCallbacks.push(() => {
try {
setTimeout(() => {
const result = onFulfilled(self.value);
result instanceof Promise
? result.then(
(res) => resolve(res),
(rej) => reject(rej)
)
: resolve(result);
});
} catch (error) {
reject(error);
}
});
self.onRejectedCallbacks.push(() => {
setTimeout(() => {
const result = onRejected(self.reason);
result instanceof Promise
? result.then(
(res) => resolve(res),
(rej) => reject(rej)
)
: reject(result);
});
});
}
if (self.status === FULFILLED) {
try {
setTimeout(() => {
const result = onFulfilled(self.value);
result instanceof Promise
? result.then(resolve, reject)
: resolve(result);
});
} catch (error) {
reject(error);
}
}
if (self.status == REJECTED) {
try {
setTimeout(() => {
const result = onRejected(self.reason);
result instanceof Promise
? result.then(resolve, reject)
: reject(result);
});
} catch (error) {
reject(error);
}
}
});
}
catch(onRejected) {
return this.then(null, onRejected);
}
static resolve(value) {
if (value instanceof Promise) {
return value;
} else {
return new Promise((resolve, reject) => {
resolve(value);
});
}
}
static reject(reason) {
return new Promise((resolve, reject) => reject(reason));
}
all(promises) {
return new Promise((resolve, reject) => {
const result = [];
let count = 0;
for (let i = 0; i < promises.length; i++) {
const promise = Promise.resolve(promises[i]);
promise
.then((res) => {
result[i] = res;
count++;
if (count === promises.length) {
resolve(result);
}
})
.catch((error) => {
reject(error);
});
}
});
}
trace(promises) {
return new Promise((resolve, reject) => {
promises.forEach((p) => {
const promise = Promise.resolve(p);
promise
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
});
}
allSetted(promises) {
return new Promise((resovle, reject) => {
try {
const result = [];
let count = 0;
for (let i = 0; i < promises.length; i++) {
promises[i]
.then((res) => {
result[i] = {
status: "fulfilled",
value: res,
};
count++;
if (count === promises.length) {
resovle(result);
}
})
.catch((err) => {
result[i] = {
status: "rejected",
value: err,
};
count++;
if (count === promises.length) {
resovle(result);
}
});
}
} catch (error) {
reject(error);
}
});
}
}
- 获取页面中所有的
tagName
function func() {
return [
...new Set([...document.querySelectorAll("*")].map((el) => el.tagName)),
].length;
}
- 数组乱序
const func = (arr) => {
return arr.sort(() => (Math.random() > 0.5 ? 1 : -1));
};
- 对象扁平化
const objTmp = {
a: { b: { c: 1 } },
d: 2,
e: [3, { f: 4, g: [5] }, [6, 7]],
h: 8,
};
function fattenObj(obj, res = {}, prevKey = "", isArr = false) {
for (let [key, value] of obj.entries()) {
if (typeof value === "object" && value !== null) {
const theKey = isArr ? prevKey + "[" + key + "]" : prevKey + key;
fattenObj(value, res, theKey);
}
if (Array.isArray(value)) {
const theKey = isArr ? prevKey + "[" + key + "]" : prevKey + key + ".";
fattenObj(value, res, theKey, true);
}
const theKey = isArr ? prevKey + "[" + key + "]" : prevKey + key;
res[theKey] = value;
}
}
const res = fattenObj(objTmp, {});
console.log("res", res);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix