1. use !! Convert a variable to a boolean type

Sometimes we need to check whether some variables exist , Or does it have a valid value , So their values are treated as true. For doing this kind of inspection , You can use it !!( Double negative operator ), It can automatically convert any type of data to Boolean , Only then will the variable return false:0,null,”“,undefined or NaN, All other values are returned true.
2. use + Replace variables with numbers
This is super easy to replace , But she only applies to numeric strings , Or it will return NaN( It's not a number ). Look at this example :
function toNumber(strNumber){
return +strNumber;
}
console.log(“1234”);//1234
console.log(“ABCD”);//NaN
This replacement operation can also be used on date, In this case , It will return the timestamp :
console.log(+ new Date());//1462548741
3. use || Set default value
stay ES6 There is a default parameter function in . To simulate this feature in older browsers , You can use it ||
(or operator ), And take the default value as its second parameter . If the first parameter returns false, The second parameter will be returned as the default value . Look at the following example :
function User(name,age){
this.name = name || “David”;
this.age = age || “25”;
}
var user1 = new User();
console.log(user1.name);//David
console.log(user1.age);//25
var user2= new User(“Barry”,”10”):
console.log(user2.name);//Barry
console.log(user2.age);//10
4. Cache in loop array.length
var length = array.length;
for(var i = 0; i < length; i++){
console.log(array[i]);
}
For simplicity , It can be written like this
for(var i = 0,length = array.length; i < length; i++){
console.log(array[i]);
}
5. Gets the last element of the array

Array.prototype.slice(begin,end) Can be used to crop arrays . But if the end parameter is not set , This function automatically sets the end Set to array length value . If you will begin If it is set to a negative number , You can get the countdown from the array .
var array = [1,2,3,4,5];
console.log(array.slice(-1));//[5]
console.log(array.slice(-2));//[4,5]
console.log(array.slice(-3));//[3,4,5]
6. Array truncation

This technique can lock the size of the array , This is useful for removing a fixed number of elements from an array . for example : The inclusion of an array 10 Elements , But you just want to take out the first five values , You can set the array.length
= 5 To truncate the array . example :
var array = [1,2,3,4,5,6];
console.log(array.length);//6
array.length = 3;
console.log(array.length);//3
console.log(array);//[1,2,3]
7. replace all
var str = “abacc”;
console.log(str.replace(/a/”1”));//1bacc
console.log(str.replace(/a/g/,”1”));//1b1cc
8. Merge array
If you need to merge two arrays , This can be done :
var arr1 = [1.2];
var arr2 = [3,4];
console.log(arr1.push.apply(arr1,arr2));//[1,2,3,4]
9. hold NodeList Convert to array
Just use this function :[].slice.call(elements);
10. Shuffle array elements
var list = [1,2,3];
console.log(list.sort(function(){
return Math.random - 0.5
}));//[2,1,3]

Technology
©2019-2020 Toolsou All rights reserved,
el-select Get selected label value mysql No backup recovery el-select At the same time label and value value @Repository The role of annotations SpringBoot JpaRepository Database addition, deletion, modification and query python primitive -- lock Lockfio Use details python in switch_to_alert Usage of Determine whether the current object is an array 4 Methods vue-countTo Complete operation