判断数组的深度
function getMaxDeepArr(arr){
let curDep = 1
let res = []
function forArr(arr){
for (i of arr){
if(Array.isArray(i)){
curDep+=1
forArr(i)
}
}
res.push(curDep)
curDep-=1
console.log(arr,'到底了')
}
forArr(arr)
return Math.max(...res)
}
console.log(getMaxDeepArr([1,3,3,[4,[6],7,[5,6,7,43,[23,4]]],[1,2]]))
function fn(arr, dep = 0) {
if (!Array.isArray(arr)) return dep;
return Math.max(...arr.map((i) => fn(i, dep + 1)));
}
console.log(fn([[1, 2],1, 3, 3, [4, [6], 7, [5, 6, 7, 43, [23, 4]]], [1, 2]]));
function s(arr) {
let i = 1;
function l(arr) {
if (arr.find((item) => Array.isArray(item))) {
i += 1;
l(arr.flat());
}
}
l(arr);
return i;
}
console.log(s([[1, 2],1, 3, 3, [4, [6], 7, [5, 6, 7, 43, [23, 4]]], [1, 2]]));
let peoples = [
{ n: 'p1', w: 100 },
{ n: 'p2', w: 200 },
{ n: 'p3', w: 100 },
]
let rand = function (p) {
let allW = 0
p.forEach(element => {
allW+=element.w
});
let randNum = Math.floor(Math.random() * (allW - 1 ) ) + 1
console.log(randNum)
let helpNum = 1
let res = p.filter(e =>{
console.log(helpNum,e.w+helpNum)
if(helpNum<=randNum && randNum< e.w+helpNum){
helpNum+=e.w
return true
}else{
helpNum+=e.w
return false
}
})
console.log(res[0])
return res
}
rand(peoples)
最长无重复子串
function lengthOfLongestSubstring(str){
if(str.length == 0) return 0
let max = 0
let arr = []
for (let s of str){
if(!arr.includes(s)){
arr.push(s)
}else{
max = Math.max(max,arr.length)
arr=[]
arr.push(s)
}
}
return max
}
深拷贝
function deepClone(value) {
if (!isObjOrArr(value)) return value;
let res = Array.isArray(value) ? [] : {};
for (const key in value) {
if (isObjOrArr(value[key])) {
res[key] = deepClone(value[key]);
} else {
if (typeof value[key] === 'function') {
res[key] = new Function(`return ${value[key].toString()}`)();
} else {
res[key] = value[key];
}
}
}
return res;
}
function isObjOrArr(value) {
return ['[object Object]', '[object Array]'].includes(Object.prototype.toString.call(value));
}
发布订阅模式
class Event {
constructor() {
this.events = {};
}
on(key, cb) {
this.events[key] = cb;
}
emit(key, params = {}) {
this.events[key] && this.events[key](params);
}
remove(key) {
this.events[key] = null;
}
}
实现retry
async function reTry(fn, interval, times) {
let allTimes = 0;
while (allTimes < times) {
try {
const res = await fn();
return res;
} catch (error) {
allTimes++;
if (allTimes === times) {
throw error;
} else {
await new Promise((res, rej) => setTimeout(res, interval));
}
}
}
}
版本号对比
function versionCompare(v1, v2) {
let v1 = v1.split('.'),
v2 = v2.split('.');
let len = Math.max(v1.length, v2.length);
for (var i = 0; i < len; i++) {
var a = parseInt(v1[i]) || 0,
b = parseInt(v2[i]) || 0;
if (a < b) return -1;
if (a > b) return 1;
}
return 0;
}
console.log(versionCompare('1.1', '1.1.0.1'));
数组扁平化
Array.prototype.allFlat = function () {
let arr = this;
let hasArr = true;
while (hasArr) {
arr = [].concat(...arr);
hasArr = arr.some(Array.isArray);
}
return arr;
};
手写reduce
Array.prototype.myReduce = function (cb, ...args) {
const isInit = args.length > 0;
const init = args[0];
let res = isInit ? init : this[0];
let index = isInit ? 0 : 1;
let len = this.length;
for (let i = index; i < len; i++) {
res = cb(res, this[i]);
}
return res;
};