This is the first day of my participation in the August Challenge. For details, see:August is more challenging

Personal technical blog, wechat official account, video account and design portfolio of the blogger

1, push to add an array item at the end of the array change the original array

  • Return Returns the number of added data (length)
  • You can pass more than one parameter
let arr = [1.2.3.4.5.6]; 
arr.push(7);  //return 7 
console.log(arr);  / /,2,3,4,5,6,7 [1]
arr.push("l"."o"."v"."e");  / / [1,2,3,4,5,6,7, "l", "o", "v", "e"]
Copy the code

2,popDeletes the last item in the array

  • Return the removed data
  • No need to pass arguments (just delete the last one)One at a time
let arr = [1.2.3.4.5.6.7]; 
console.log( arr.pop() ); // 7 Returns the removed data 7
console.log(arr); // [1, 2, 3, 4, 5, 6] 7 is deleted
Copy the code

3,unshiftTo add an array item (data) to the beginning of an array

  • Return Returns the number of added data (length)
  • More than one parameter can be passed
let arr = [1.2.3.4.5.6.7]; 
arr.unshift("l"."o"."v"."e"); //return 11 
console.log(arr); // ["l","o","v","e",1, 2, 3, 4, 5, 6, 7]
Copy the code

4,shiftRemoves the first item from the array

  • Return the removed data
  • No need to pass parameters (just delete the first one no matter who you are)One at a time
let arr = [1.2.3.4.5.6.7]; 
arr.shift(); // return 1 
console.lig(arr); / /,3,4,5,6,7 [2]
Copy the code

5,sliceSlice an array by subscript (without changing the original array)

  • The parameter (subscript/index value) is closed left and opened right
  • There is only one parameter and by default this index starts all the way to the end
let arr = [1.2.3.4.5.6.7];
let arr1 = arr.slice(4.6);  //4, 5, 6 to the right, so you can't take 6
console.log(arr1);  // [5,6] corresponds to subscript 4, 5
Copy the code

6,splice( index ,num ,new…)replace

  • The first parameter index => Start subscript This parameter is mandatory
  • The second parameter num => Number of left-right substitutions This parameter is mandatory
  • The new => parameter is optional
let arr = [1.2.3.4.5.6.7]; 
let arr1 = arr.splice(2.3."l"."o"."v"."e"); 
console.log(arr1); // the clipped array returned by [3,4,5,]
console.log(arr);  // [1, 2, "l","o","v","e", 6, 7]
Copy the code

Can replace the above 5 methods

/ / after a push
arr.splice(arr.length,0."l") // Delete 0 from the last one without splicing

/ / after the pop
arr.splice(arr.length-1.1) // Delete from the first one without splicing

/ / the unshift before
arr.splice(0.0."l")  // Delete 0 from the first one

/ / before the shift
arr.splice(0.1) 

For example, slice(3,5) close left and open right actually cut 3, 4
arr.splice(3.2)
Copy the code

7,concatArray concatenation returns the concatenated array

  • The body to concatenate goes first, and the array to concatenate goes inside the method
  • You can concatenate multiple arrays to return a new array, requiring variable acceptance
let arr = ["l"]; 
let arr1 = ["o"] 
let arr2 = ["v"] 
let arr3 = arr.concat(arr1, arr2); 
// arr3 = arr.concat(["o"], ["v"])
// arr3 = arr.concat("o", "v")
console.log(arr3) 

Copy the code

Eight,joinArray variable stringnecessary

  • “” Empty instead,
  • “+” plus instead,
  • “” Space instead,
let arr = [1.2.3.4.5."l"] 
let str = arr.join() / / "1, 2, 3, 4, 5, l"
let str1 = arr.join("") // "12345l" 
let str2 = arr.join("+") // "1+2+3+4+5+l" 
let str3 = arr.join("") // "1 2 3 4 5 l"
Copy the code

9,sortSort modifies the data sort in the original array

// Sort by first character encoding order
let arr = [9.5.3.1.7.8.4] 
arr.sort() 
console.log(arr) //[1, 3, 4, 5, 7, 8, 9]
Copy the code
  • Value > 0 does not change the order
  • Positive order (bubble sort)
  • A – B ascending from small to large
let arr = [9.5.3.1.7.8.4]; 
arr.sort(function(a, b){ 
    return a - b; 
}) 
console.log(arr); // [1, 3, 4, 5, 7, 8, 9]
Copy the code
  • Reverse order (bubble sort)
  • B – A descending from large to small
let arr = [9.5.3.1.7.8.4]; 
arr.sort(function(a, b){ 
    return b - a; 
}) 
console.log(arr) // [9, 8, 7, 5, 4, 3, 1]
Copy the code
  • Sort json object arrays
    var data = [{
            name: "Overseas Business Division".value: 0.58
        }, {
            name: "Domestic".value: 0.36
        }, {
            name: Internet Hub.value: 0.78
    }]; 

// function compare(a,b){
// return b.value-a.value;
/ /}

    / / es6 writing
    let compare = (a,b) = > b.value-a.value;
    data.sort(compare);
    console.log(data);
Copy the code