Implement a batch request, multiRquest(urls, maxnum); (20210608).
- The maximum concurrency tree is maxnum
- Each time a request returns, a gap is left for new requirements to be added
- So when the request completes, the results are exported in order of urls
class multiRquest {
constructor(urls, num) {
this.len = urls.length;
this.results = [];
this.curUrls = urls.slice(0, num);
this.restUrls = urls.slice(num, this.len);
this.curUrls.forEach(url= > {
this.ajax(url); })}ajax(url) {
fetch(url).then(res= > {
this.results.push(url);
console.log(this.restUrls)
if(this.restUrls.length > 0) {
let nextUrl = this.restUrls.shift();
this.ajax(nextUrl); }}}})const req = new multiRquest([1.2.3.3.4.5.6.7].2)
Copy the code
2. Manually implement Array.flat(ARR, NUM); (20210609).
class arrayFlat {
constructor(arr, num) {
if(!Array.isArray(arr)) {
console.error('Please enter the correct array format! ')
return;
}
this.index = 0;
this.num = num;
this.result = [];
this.trueRes = arr.flat(num);
if(arr.length) {
this.index++;
arr.map(ar= > {
this.flatItem(ar); }}})flatItem(item) {
if(this.num >= this.index) {
if(Array.isArray(item)) {
this.index++;
item.map(i= > {
this.flatItem(i);
});
return;
};
}
this.result.push(item); }}let arr = new arrayFlat([1.2.3[1.2[2.3]]].1);
Copy the code
3, handwritten promise.all; (20210610).
let promiseAll = function(arr) {
return new Promise((resolve,reject) = > {
let len = arr.length;
let count = 0;
let result = [];
for(let i = 0; i < len; i++) {
Promise.resolve(arr[i]).then(res= > {
count++;
result[i] = res;
if(count == len) {
returnresolve(result); }},err= >{ reject(err); })}})}Copy the code
4, shake and throttle
// Buffering: when an event requires a delayed callback, restart the timer after multiple events are triggered;
function debounce (delay, cb) {
let timer = null;
return function(. arg) {
if(timer) {
clearTimeout(time);
}
timer = setTimeout(function() {
cb.apply(this,arg); }, delay); }}// throttling: The event is fired multiple times, we intercept the event and execute it only once within 10 seconds
function throttle(delay, cb) {
let pretime = 0;
return function(. arg) {
let now = +new Date(a);if(now - pretime > delay) {
pretime = now;
cb.call(this.arg); }}}Copy the code
5. Array deduplication
function unique(arr) {
if(Array.isArray(arr)) {
console.error('Please enter the correct data format! ');
return;
}
let obj = {};
let result = [];
arr.length && arr.map(item= > {
if(! obj[item]) { obj[item] =1; result.push(item); }})return result;
}
function unique2(arr) {
if(Array.isArray(arr)) {
console.error('Please enter the correct data format! ');return;
}
return [...new Set(arr)];
}
Copy the code
6, array sort
// Bubble sort
const bubble = (arr) {
let len = arr.length;
for(let i = len - 1; i > 1; i--) {
for(j = 0; j < i - 1; j++) {
if(arr[j+1] > arr[j]) {
let res = arr[j];
arr[j+1] = arr[j]; arr[j] = res; }}}return arr;
}
// Select sort
const selectSort = (arr) = > {
let len = arr.length;
for(let i = 0; i < len; i++) {
for(let j = i; j < len; j ++) {
if(arr[j] < arr[i]) { [...arr[i], arr[j]] = [...arr[j], arr[i]]; }}}return arr;
}
// Quicksort
const quickSort = (arr) = > {
if(arr.length < 2) {
return arr;
}
let len = arr;
let left = [], right = [];
let first = arr[0];
for(let i = 1; i < len; i++) {
if(arr[i] > first) {
right.push(arr[i])
} else {
left.push(arr[i])
}
}
return quickSort(left).concat(first, quickSort(right));
}
Copy the code