var arr = [1, 2, 3, [4, 3, [2, 7], 2], 5, [5, 9, 10], 7];
最简单的方式递归
function func(user){
for(var i=0;i<user.length;i++){
if(Array.isArray(user[i])==false){
// arr.push(user[i]);
}else{
func(user[i]);
}
}
}
func(arr)
console.log(arr);
防抖和节流
节流
时间戳
var content = document.getElementById("content");
var num = 1;
var timeNew = 0;
content.onmousemove = function(){
var timeOld = Date.now();
if(timeOld-timeNew>1000){
timeNew = timeOld;
content.innerHTML = num++;
}
}
延时器
var content = document.getElementById("content");
var num = 1;
var time;
content.onmousemove = function(){
if(!time){
time = setTimeout(()=>{
time = null;
content.innerHTML = num++;
},1000)
}
}
原理 定时器或延时器
var content = document.getElementById("content");
var num = 1;
let times=false;
// let obj = false;
防抖
content.onmousemove = function (){
let obj = times;
clearTimeout(times);
times = setTimeout(()=>{
times = null;
},1000)
if(!obj){
console.log(typeof !obj);
content.innerHTML = num++;
}
};
let a = 10;
let b = !a;
console.log(!a);
console.log(!b);
console.log(!null);
console.log(!undefined);
console.log(!NaN);
console.log(!"aaaa");
console.log(!{});
console.log(![]);
数组扁平化–将多维数组变为一维数组
var arr = [1, 2, 3, [4, 3, [2, 7], 2], 5, [5, 9, 10], 7];
function func(user){
let arr1 = [];
for(var i=0;i<user.length;i++){
if(Array.isArray(user[i])==false){
arr1.push(user[i]);
}else{
arr1 = arr1.concat(func(user[i]))
}
}
arr = arr1;
return arr;
}
function func(user){
let arr1 = [];
user.forEach((item)=>{
Array.isArray(item)==false?arr1.push(item):arr1 = arr1.concat(func(item))
})
arr = arr1;
return arr;
}
func(arr);
console.log(arr);
函数柯里化
function curryingAdd(x) {
return function (y) {
return x + y
}
}
function binds() {
return function (x,y) {
return x + y
}
}
console.log(add(1,3));
console.log(curryingAdd(1)(5));
console.log(binds()(2,4));