Direct dry goods:

1. Array sort.

The sort() method sorts arrays alphabetically:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
Copy the code

Descending sort an array using the same technique:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
Copy the code

2. Unduplicate array elements

Use ES6 Set to remove weights (most commonly used in ES6)

Function unique (arr) {return Array. The from (new Set (arr)]} var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]; console.log(unique(arr)) //[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}]Copy the code

Use for to nest for and then splice to deduplicate it

function unique(arr){ for(var i=0; i<arr.length; i++){ for(var j=i+1; j<arr.length; J ++){if(arr[I]==arr[j]){// if(arr[I]==arr[j]); j--; } } } return arr; } var arr = [1, 1, 'true', 'true, true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN,' NaN ', 0, 0, 'a', 'a',{},{}]; Console. log(unique(arr)) //[1, "true", 15, false, undefined, NaN, NaN, "NaN", "a", {...}, {...}] //NaN and {} are nullCopy the code

More array to heavy: segmentfault.com/a/119000001…

3. Sum the array recursively

Recursive summation:

Var arr = [1, 2, 3]; function sum(arr) { if(arr.length == 0){ return 0; } else if (arr.length == 1){ return arr[0]; } else { return arr[0] + sum(arr.slice(1)); } } console.log(sum(arr)); / / 6Copy the code

For loop summation

Var arr = [1, 2, 3]; function sum(arr) { var s = 0; for (var i = 0; i<arr.length; i++) { s += arr[i]; } return s; } console.log(sum(arr)); / / 6Copy the code

4. Deep copy and shallow copy

Deep copy and shallow copy only apply to complex objects such as Objects and arrays. In simple terms, a shallow copy copies the properties of an object at only one level, while a deep copy recursively copies all levels. A shallow copy:

Var obj = {a:1, arr: [2,3]}; var shallowObj = shallowCopy(obj); function shallowCopy(src) { var dst = {}; for (var prop in src) { if (src.hasOwnProperty(prop)) { dst[prop] = src[prop]; } } return dst; }Copy the code

Because shallow copying only copies the attributes of an object sequentially, not recursively, and JavaScript stores objects as addresses, shallow copying causes obj.arr and shallowobj.arr to point to the same memory address.

shallowObj.arr[1] = 5;
obj.arr[1]   // = 5
Copy the code

Deep copy is different. It not only copies each attribute of the original object one by one, but also copies the objects contained in each attribute of the original object recursively to the new object.

It is important to note that if the object is large and has many levels, deep copy can cause performance problems. When you encounter scenarios that require deep replication, consider alternatives. In actual application scenarios, shallow replication is more common.

5. Do a 10-second countdown

<! DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <script type="text/javascript"> function daojishi() { var starttime=document.getElementById("id2").innerText; if(starttime==0) { return ; } setTimeout("daojishi()",1000); starttime--; document.getElementById("id2").innerText=starttime; } < / script > < body > < h5 id = "id2" > 10 < / h5 > < button id = "id1" onclick = "daojishi ()" > start the countdown < / button > < / body > < / HTML >Copy the code

6. Use and differences between setTimeout() and setInterval()

SetTimeout () method

This method is supported by all browsers. SetTimeout () is a method that belongs to the window, but we omit the top-level object name window, which is used to set a time when the specified method will be executed.

<! DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script> var x = 0; function countSecond() { x = x+1; document.haorooms.haoroomsinput.value = x; setTimeout("countSecond()", 1000) } </script> </head> <html> <body> <form name="haorooms"> <input type="text" name="haoroomsinput" value="0" size=4 > </form> <script> countSecond(); </script> </body> </html>Copy the code

The setInterval() method calls a function or evaluates an expression at a specified period in milliseconds. The setInterval() method will keep calling the function until

ClearInterval () is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> <script> function test(){ this.name = "setInternal"; this.waitMes = function(){ var that = this; setInterval(function(){ alert(that.name); }, 3000); } } var te = new test(); te.waitMes(); </script> </html>Copy the code

As can be seen from the above, the main differences between setTimeout and setInterval are as follows:

1) setTimeout is run only once, that is to say, the specified code will be triggered to run after the set time expires, and the specified code will be finished after the run. If the same setTimeout command is run again in the running code, it can be looped. To loop, the function itself calls setTimeout() again. Setinterval, on the other hand, runs in a loop, firing the specified code at a specified interval. This is a real timer.

2) SetInterval is simple to use, while setTimeout is more flexible. It can exit the cycle at any time and can be set to run at an unfixed time interval, such as 1 second for the first time, 2 seconds for the second time, and 3 seconds for the third time.