Article directory
async await There are synchronous operations in asynchronous
After executing one, execute the other is synchronous, such as promise().then(),
all executed together are asynchronous
. The difference between synchronous and asynchronous operations is whether it hinders the execution of subsequent codes.
The following example is synchronous, execute test2 and finally test3 after executing test, (there is a timer in test2)
join 、split、splice
join - - array to string
split - - string to string array
splice
The splice method is used for arrays or pseudo-arrays. Depending on the number and form of parameters, specified elements can be deleted, inserted, or replaced in the array.
a parameter
- a parameter (delete)
When the splice method has only one parameter (i), it means to delete all elements with index i and after i in the array. Returns the deleted element, the array is modified in-place. Among them, the parameter i is an integer.
For the case of i:
i is a non-negative integer: delete all elements at the index i and after i in the array.
i is a negative integer: the index is calculated from the back to the front, the index of the last digit is -1, and the penultimate digit is -2, and so on. Delete i and all elements after i.
eg: delete the last three elements of the array
var a = [1, 2, 3, 4, 5]
a.splice(-3)
console.log(a) // [1, 2]
empty array
var a = [1, 2, 3, 4, 5]
a.splice(0) // 或 a.splice(-5)
console.log(a) // []
- two parameters (deleted)
arr.splice(i, j)
When the splice method has two parameters, both parameters must be integers. It means to delete from the index i in the array, and delete j elements in total.
eg: delete the first 3 elements in the array
var a = [1, 2, 3, 4, 5]
a.splice(0, 3)
console.log(a) // [4, 5]
Delete 2 elements starting from index -2
var a = [1, 2, 3, 4, 5]
a.splice(-2, 2)
console.log(a) // [1, 2, 3]
-
Three parameters or more (replace, add)
a.splice(i, j, e1, e2, ...)
i: integer, indicating the starting position of the index
j: integer, indicating the number of deletions ( when j is 0, it means adding )
e1, e2, ...: delete the corresponding the element to add after the element
When the splice method has 3 parameters, it means to delete j elements starting from the index i position, and then add e1, e2, ... at the i position, return the deleted element, and modify it in place.
If j is 0, it means that no element will be deleted, and the element will be inserted from the position before i.
If j > 0, it means that j elements will be deleted from position i (including position i), and then inserted from the position after i.
eg: replace the element at index position 2 with the value 'aaa'
var a = [1, 2, 3, 4, 5]
a.splice(2, 1, 'aaa')
console.log(a) // [1, 2, 'aaa', 4, 5]
Insert elements 'a', 'b', c into the array at index 1
var a = [1, 2, 3, 4, 5]
a.splice(1, 0, 'a', 'b', 'c')
console.log(a)
// [1, 'a', 'b', 'c', 2, 3, 4, 5]