Common array methods

The length of the array

Arr. length Gets the length of the array

Arr. Length-1 gets the last bit of the array

Multidimensional array

var arr=["a"["b"."c"]].console.log(arr[1] [1]);// Get c

// A one-dimensional array has one [], and a multidimensional array has n []

Array elements are accessed by index, which starts at 0

//arr[1]==["b","c"] arr[1][1]=="c"
Copy the code

Array creation and array.of () methods

// Create with new
var arr=new Array(1.2.3.4);

var arr1=new Array(2);
//[empty,empty] indicates that the length of the array is 2 and there are two empty elements in the array

// Create with a literal
var arr1=[5.6.7.8]
Copy the code
// Modify the value of an array element
var arr=[1.2.3];
var b =arr;
b[1] =4;/ / [1, 3]
Copy the code

Arrays are reference types that use the same memory address, which is the property of reference types, which are shallow copies, and value types, which make a copy of memory, which is a deep copy

        var arr=[1.2.3.4];
        arr[6] =7;
        console.log(arr);/ / 7] [1, 2, 3, 4,,,
        // Beyond the length of the array, a value will be added at that location, but the previous value will be empty
Copy the code

Array.of()

       ArrayOf (parameters1, the parameter2...) You can put multiple values of different types and create a variable number of new arraysvar arr1=new Array(6);/ / [6]
       var arr2=new Array(1.2.3);/ / [1, 2, 3]
       conlose.log(arr1.length,arr);/ / 1 6
  // array.of () creates an Array of length 1 and value 6
Copy the code

Difference between array.of () and Array()

The difference between array.of () and Array is that it handles integer arguments:

Array.of(7) creates an Array with a single element of 7, while Array(7) creates an empty Array of length 7

Note: This refers to an array with seven empty Spaces, not seven undefined ones

Array type detection

Array. IsArray (parameters)

// Return true or false
console.log(Array.isArray([]));// true
Copy the code

The instanceof operator checks if it is an array

// Return true or false
var arr=[1.2];
arr instanceof Array // true
Copy the code

Array.isArray takes precedence over Instanceof

Array type conversion

Array to string

var arr=[1.2.3]; The toString () methodconsole.log(arr.toString());/ / string type 1, 2, 3

String() methodconsole.log(String(arr));/ / 1, 2, 3Join (delimiter)// Put all the elements of the array into a string. The elements can be delimited by a specified delimiter, omit arguments, and are separated by commas by default
console.log(arr.join());/ / 1, 2, 3
console.log(arr.join("|"));/ / 1 | 2 | 3
Copy the code

The return value of the above methods is a string

A pseudo-array is converted to a true array

Array.from() converts a pseudo-array to a true arrayvar str=123;
Array.from(str);/ / [1, 2, 3]
Copy the code

Array add, delete, change, check

increase

Push () and the unshift ()

var a=[1.2];

push()
a.push(3);/ / [1, 2, 3]
a.push(4.5."q");/ / [1, 2, 3, 4, 5, "aa"]
/* Write arguments to the element appended to the end of the array. You can write multiple arguments to the element appended to the end of the array

unshift()
a.unshift("bb");/ / / "bb", 1, 2, 3, 4, 5, "aa"]
/* Write parameters to the first element of the array (). You can write multiple parameters to the first element of the array
Copy the code

delete

Pop (), and the shift ()

Pop () removes the last element of the array a.pop();/ / (" bb ", 1, 2, 3, 4, 5)

/* Delete only one element at a timeShift () deletes first element of array a.hift ();/ / [1, 2, 3, 4, 5]

/* Delete only one element at a time
Copy the code

Pop and push are stack operations and unshift and shift are queue operations

Slice () array interception

/ / the subscriptSlice [start,end) returns truncated new array unable to swap start and end position arguments do not write truncated all array negative values from back to front [-2, -1You can convert an array of classes into a new array that is left closed and right openvar a=[1.2.3.4.5]
  console.log(a.slice());/ / 1, 2, 3, 4, 5
  console.log(a.slice(1.3));/ / 2, 3
  console.log(a.slice(-3, -1));/ / 3, 4

Copy the code

Splice () array deleted

Splice ([start(add or remove position)], number of inserted elements) returns an array of deleted elements, changing when the original array has no arguments to return an empty array with only one argument, from the current position to the end of the second argument0Is not deletedvar a = [1.2.3.4.5]
        var arr = [1.2.5.3.15.20];
        console.log(a.splice());/ / []
        console.log(a.splice(0.5));/ / [1, 2, 3, 4, 5]
        console.log(arr.splice(1.1."11")); [2]
        console.log(arr);/ / 5,3,15,20] [1, "11",
Copy the code

An iterative method of an array

In iteration, if there are unnecessary parameters, you can omit them

  1. array.forEach()

In forEach, a return does not terminate the iteration

// The callback function
array.forEach(function(currentValue,inedx , arr){})
/*currentValue: the value of the current item in the array index: the index of the current item in the array arr: the object itself */
 arr.forEach(function(value,index,arr){
            console.log(value);
            console.log(arr[index]);
        })
Copy the code
  1. array.map()
array.map(function(currentValue,inedx , arr){})
/* Returns a new array, each element being the result of the callback */
     var arr = [1.2.5.3.15.20];
       // map iterates through each array and returns a new array
        var map=arr.map(function(num){
            return num+1;
        })
        console.log(map);/ /,3,6,4,16,21 [2]
Copy the code
  1. array.filter()It is used to filter an array to find elements that match the criteria

In a filter,return does not terminate the iteration

array.filter(function(currentValue,inedx , arr){})
/* Create a new array, which is used to filter the array, find the elements that meet the criteria directly return a new array, return all elements that meet the criteria */
 var arr = [1.2.5.3.15.20];
 var newArr = arr.filter(function (value, index, arr) {
 // Returns a new array, receiving the array with a variable
            return value >= 10;
        })
        console.log(newArr);/ / 15, 20
Copy the code
  1. array.some()Finds the existence of an element in the array that meets the condition
array.some(function(currentValue,inedx , arr){})
/* Returns true or false to check for the existence of an element in the array that meets the condition
 // Returns a value, received with a variable
  var arr = [1.2.5.3.15.20];
  var flag=arr.some(function(value,index){
            return value>=10;
        })
        console.log(flag);// true
Copy the code
  1. array.every()Determine if each element passes the test
        
array.every(function(currentValue,inedx , arr){})
// Determine if each element passes the test, false if one element fails
// The return value is true or false

 var arr = [1.2.5.3.15.20];
 // Returns a value, which is received with a flag variable
 var flag=arr.every(function(value,index,arr){
            return value>=20;
        })
        console.log(flag);//false
Copy the code
  1. array.find()Used to find the first element that matches the condition
Array. find(function(currentValue,inedx, arr){}) /* Var arr = [1, 2, 5, 3, 15, 20]; var arr = [1, 2, 5, 3, 15, 20]; var flag=arr.find(function(value,index,arr){ return value>=15; }) console.log(flag);Copy the code
  1. array.findindex()Finds the index of the first element that matches the criteria
array.findindex(function(currentValue,inedx , arr){})
/* The index used to find the first element that meets the criteria, returns -1 if no argument is found, you can write no or only a */
     var arr = [1.2.5.3.15.20];
     var flag=arr.findIndex(function(item){
            return item>15;
        })
        console.log(flag);/ / 5
Copy the code

Checks whether the specified value is included

arr.includes()


// Determine if an array contains the given value, return Boolean
[1.2.3].includes(2);//true
[1.2.3].includes(4);// false
Copy the code

A grammar

. Arr splits an array or object into comma-separated sequences of parameters

var arr=[1.2.3]; . arr;/ / 1, 2, 3
console.log(... arr);//1, 2, and 3 are used as console.log parameter separators, so output 1, 2, and 3
Copy the code

Merge array

Arr. Concat (Array)(normal method)

  var arr = [1.2.5.3.15.20];
  var b=["11"];
  console.log(arr.concat(b));//[1, 2, 5, 3, 15, 20, "11"]
  
  // Returns a new array
Copy the code

A grammar

Methods avar a=[1.2.3];
var b=[4.5.6];
var c=[...a,...b];/ / [6]Method 2 Amy polumbo ush (... b);var a=[1.2.3];
var b=[4.5.6]; a.push(... b);/ / 6
Copy the code

Array index

arr.indexOf()

var a=["aa"."bb".3."bb"];
a.indexOf("bb");// return index 1

// Start from the front
// Only the first index that meets the condition is returned
// If no element is found in the array, -1 is returned
Copy the code

arr.lastindexOf()

var a=["aa"."bb".3."bb"];
a.lastindexOf("bb");// return index 3

// Start from the back
// If no element is found in the array, -1 is returned
Copy the code

Sort an array

  1. Flip the array

reverse()

var a=[1.2.3];
a.reverse();/ / [3, 2, 1]
Copy the code
  1. Array sort (bubble sort)

sort()

var a=[4.2.1.6];
a.sort();/ / 1,2,4,6
// Note: Sort can only be implemented by unit number sort, when it comes to multi-digit sort, sort by the first digit size

// the perfect solution to sort()
var a=[42.21.11.6];
a.sort(function(a,b){
    return a-b;/ / ascending
    return b-a;/ / descending
})
Copy the code