Array removes duplicate elements
Function fn(arr) {let newArr = [] for (let I = 0; i < arr.length; I ++) {if (newarr.indexof (arr[I]) === -1) {newarr.push (arr[I])}} return newArr = 0; i < arr.length; i++){ for(let j = i + 1; j < arr.length; J++) {if (arr [I] = = = arr [j]) {arr. Splice (j, 1) j -}}} return arr} / / output [1, 2, 33, 4, 5, 3, 6]Copy the code
Flatten the array and remove duplicate elements, and output in ascending order
let arr = [1, [1, [2]], [3, 4], 7, 4, [2, 3, [4, 5, [2, 2, 1, 5, 4, 3, Function fn(arr) {let newArr = arr.flat(Infinity) newArr = [... new Set(newArr)] return newarr.sort ((a, Function fn(arr) {let STR = json.stringify (arr) let newArr = JSON.parse(`[${str.replace(/\[|\]/g, '')}]`) let arr1 = newArr.filter((item, index) => newArr.indexOf(item) === index) return arr1.sort((a, b) => a - b) } [ 1, 2, 3, 4, 5, 7, 8]Copy the code
Deep copy
Parse (json.stringify (obj)) function deepCopy(obj = {}){if(typeof obj! == "object" || obj == null){ return obj } let result if(result instanceof Array){ result = [] }else{ result = {} } for(const key in obj){ if(Object.hasOwnProperty.call(obj, key)){ result[key] = deepCopy(obj[key]) } } return result }Copy the code
Take a string, split each character of the string and rearrange it, printing all possible permutations, such as: Enter ‘ab’, print ‘ab’, ‘ba’
function fn(str){ let temp = [] if(str.length === 0) return '' if(str.length === 1) { return str }else{ let obj = {} for(let i = 0; i < str.length; i++){ let s = str[i] if(! obj[s]){ let newStr = str.slice(0,i) + str.slice(i+1,str.length) let l = fn(newStr) for(let j = 0; j < l.length; j++){ let t = s + l[j] temp.push(t) } obj[s] = true } } } return temp } console.log(fn('abc'));Copy the code
Implement bubble sort
Let arr =,22,333,2,4,66,4,3,2,3,67,5,43,7,5,4 [1] the function fn (arr) {for (let I = 0; i < arr.length; i++){ for(let j = i+1; j < arr.length; j++){ if(arr[i] > arr[j]){ let item = arr[i] arr[i] = arr[j] arr[j] = item } } } return arr } console.log(fn(arr));Copy the code
Hand write call,apply,bind
Function.prototype.myCall = function (ctx, ... args) { ctx.fn = this; ctx.fn(... args) delete ctx.fn } Function.prototype.myApply = function (ctx, args = []) { if (args && ! Array.isarray (args)) {throw (new Error(' Array '))} ctx.fn = this; ctx.fn(... args) delete ctx.fn } Function.prototype.myBind = function (ctx, ... args) { console.log(args); return (... args1) => { ctx.fn = this; ctx.fn(... args1.concat(args)) } } function a(... args) { console.log(this.name); console.log(... args); } arjun yCall ({name: "leave" XiaoXun}, 1, 2, 3, 4) arjun yApply ({name: "XiaoXun leave}", [1, 2, 3, 4]) let b = arjun yBind ({name: }, 1, 2, 3, 4) b(5)Copy the code
Handwritten ajax
function ajax({ type, url, data = null }) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open(type.toUpperCase(), url, true); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { resolve(xhr.responseText); } else {reject(' reject ')}}} data = data? JSON.stringify(data) : data; XHR. Send (data)})} / / testing ajax ({type: "get", url: "https://autumnfish.cn/api/joke/"}), then (res = > {the console. The log (res); }) .catch(error => { console.log(error); }) const data = { page: "1", count: "5" } ajax({ type: "post", url: "https://api.apiopen.top/getWangYiNews" }, data) .then(res => { console.log(res); }) .catch(error => { console.log(error); })Copy the code
If the throttle
Function debounce(fn, delay = 500) {let time = null; return function () { if (time) { clearTimeout(time) } time = setTimeout(() => { fn.apply(this, arguments); Function throttle(fn, delay = 500) {let time = null; return function () { if (time) { return } time = setTimeout(() => { fn.apply(this, arguments); time = null },delay) } }Copy the code
Handwritten isEqual
const obj1 = { a: 100, b: { c:11, x: 110, y: 200, } } const obj2 = { a: 100, b: { y: 200, x: Function isObjectOrArray(obj) {return typeof obj === 'object' &&obj! == null; } function isEqual(obj1, obj2) {if (! isObjectOrArray(obj1) || ! IsObjectOrArray (obj2)) {return obj1 === obj2} if (obj1 === obj2) {return true; If (object.keys (obj1).length! == Object.keys(obj2).length) { return false; Value for (const key in obj1) {const res = isEqual(obj1[key], obj2[key]); if (! res) return false; } return true } console.log(isEqual(obj1, obj2));Copy the code