Methods of JS arrays (super detailed, including understanding) push, pop, unshift, shift, splice, slice, concat, join, revres, indexOf, sort, filter, map

A collection of methods in an array

will change the original array:

(1) The push() method adds one or more new elements at the end of the array and returns the length of the new array.
 const arr = [1, 2, 3,]
        arr. push(4, 5, 6)
        console.log(arr);//[1,2,3,4,5,6]
        console.log(arr.push(4, 5, 6));//6
(2) The pop() method deletes the last element of the array and returns the element deleted at the end of the array
 const arr = [1, 2, 3]
        arr2 = arr. pop()
        console.log(arr);//[1,2]
        console.log(arr2);//3
(3).unshift() method Add one or more elements in front of the array and return the length of the new element
 const arr = [1, 2, 3,]
        arr. unshift(4, 5, 6)
        console.log(arr);//[4,5,6,1,2,3]
        console.log(arr.unshift(4, 5, 6));//6
(4) The shift() method deletes the first element of the array and returns the deleted element
 const arr = [1, 2, 3,]
        arr. shift()
        console.log(arr);//[2,3]
        console.log(arr.shift(4, 5, 6));//2
(5.) The splice() method deletes and modifies the array, and returns an array composed of deleted elements
 const arr=[1,2,3,4]
    //splice (delete the subscript, delete the number, add content (optional))
    cosnt arr2=arr.splice(0,2,0)
    console.log(arr)//[0,3,4]
    console.log(arr)//[1,2]
(6).slice() method cuts (intercepts) the array, and returns a new array containing the cut value, does not change the original array and can also intercept strings
"123456789".slice(2,-3);
//"3456"
Easy to understand Negative parameter slice() will add the negative parameter to the length of the string
The following negative numbers can also be understood in this way, such as -3, it will end at the fourth-to-last position, excluding the third-to-last position. */
 const arr=[1,2,3,4]
    const arr2 = arr. slice(0,3)
    console.log(arr)//]1,2,3,4]
    console.log(arr2)//[1,2,3]
(7). The concat() method merges two or more arrays, returns a new array and does not change the original array
 const arr=[1,2,3]
    const arr2=[4,5,6]
    const arr3=[7,8,9]
    const arr4=arr.concat(arr2,arr3,10)
    console.log(arr4) //[1,2,3,4,5,6,7,8,9,10]
(8).join() method converts the array into a string, will not change the original array, this method will return the converted string, separated by ‘,’ by default
 const arr=[1,2,3]
    const arr2 = arr. join()
    const arr3 = arr. join('-')
    console.log(arr)//[1,2,3]
    console.log(arr2)//1,2,3
    console.log(arr3)//1-2-3

will change the original array:

(nine). The revres() method reverses the array elements, will change the original array

(10).indexOf() method returns the index where the array element appears for the first time in the array, if not found return -1

(11).sort() method to sort the array a-b sort from small to large b-a from large to small

(12).filter() method returns the elements in the array that meet the conditions to form a new array, and the elements can only be judged by Boolean type, will not change the original array

(13.) map() method This method creates a new array. This new array is composed of the return value after calling the provided function once for each element in the original array. It can be used for operations and cannot filter elements of the element group. Will not change the original array

(14).every() method is used to judge whether the elements in the array meet the conditions. When each element meets the conditions, return true, otherwise false, will not change the original array

Fifteen.some() method Determines whether at least one of the arrays satisfies the condition. Once one is found, stop immediately and return true, otherwise false, will not change the original array

true false returns false when the array of some is empty returns true when the array of every is empty

Understanding: To prevent confusion, please remember this
Every returns failure as long as there is a failure (only look at false) because there is no false, so it is true

some: As long as there is one success, it returns success (only look at true) because there is no true, so it is false

Sixteen. forEach() method traverses the entire array, and cannot be interrupted by break

(17) The reduce() method performs four arithmetic operations on the array. The second parameter is generally set to 0. The original array will not be changed

(18).includes() method to determine whether the array exists, if it exists, it returns true, if it does not exist, it returns false

(19) The findIndex() method returns the position of the first element of the array that is passed in a test condition (function) that meets the condition. Returns -1 if there is no matching element

Note: The findIndex() method is not supported in IE 11 and earlier.

var arr2 = [1,18,2,99,666,44,66];
    var flag2 = arr2. findIndex(item => {<!-- -->
        return item > 50;
    });
    console.log(flag2) // get: 3

var arr3 = ['red','pink','green'];
    var flag3 = arr3. findIndex(item => item === 'yellow')
    console.log(flag3) // get: -1

(20) form pseudo-array into real array
document.querySelecttorAll() The element collection and arguments in the function obtained by document.querySelecttorAll() are pseudo-arrays
Array.from (original pseudo-array)
(21) split(‘separator’) converts a string to an array Use the opposite of the join method

Notice:

foreach: There is no way to terminate the loop

All methods involving interception in js include the beginning but not the end

String.substring(start subscript, end subscript) – return the intercepted string, excluding the characters corresponding to the end subscript. You can also intercept the string str.substring() without writing the end subscript, that is, intercept from the beginning to the end of the string
The method of string interception slice() substring() substr()-detailed comparison

Discrimination:
Same point:
1. All three return a new string, and the original string remains unchanged.
2. When a positive parameter is passed in, the strings returned by the three are the same, and the new strings returned are from the parameter position to the original string length position.

difference:
1. When two positive parameters are passed in, slice() and substring() return the same string. The second parameter indicates the end position, but substr() indicates the number of strings.
2. substring() does not accept negative parameters, if a negative parameter is passed in, it will be converted to 0.
3. When substr() passes in a negative parameter, it is the same as slice() and converted into parameter + string length.
4. When substr() passes in two negative parameters, it is the same as substring() and converted to 0. But substring() will sort the parameters from small to large, and substr() will not.

Intercept the string before the last few digits of the string slice() substring() The second parameter in the brackets is str.length-(write as many digits as you need)

//Because the goal is to add an item with a value of 10 to the back of each item, use the splice() method to add a 10 to the first item, and find that the original second item has reached the third item, and add a 10 to the second item, and find that the original third item has reached the fifth item, so add 10 to the odd numbered items of the array all the time