1.concatTo create a new array that willarrayConcatenate with any array or value.

let arr = ['1'.'2'.'3'.'4']
let arr2 = ['9']
let arr3 = arr.concat(arr2)
console.log(arr3);
//['1', '2', '3', '4', '9']
Copy the code

2. Pop, deletes the last element of the array and returns the last element

let arr = ['1'.'2'.'3'.'4']
var a = arr.pop();
console.log(a);/ / 4
Copy the code

3. Shift removes the first element of the array and returns the first element

let arr = ['1'.'2'.'3'.'4']
var a = arr.shift();
console.log(a);/ / 1
Copy the code

4. Splice, which replaces the data at the specified position in the array with the input data and returns the value of the replaced element. Position in the array (starting position), number to replace, value to replace

let arr = ['1'.'2'.'3'.'4']
var a3=arr.splice(1.1."789")
console.log(a3) / / / '2'
console.log(arr)// ['1', '789', '3']
Copy the code

5. Join, the data link, returns the array element linked with the input concatenator, type string

let arr = ['1'.'2'.'3'.'4']
var a5=arr.join("-");
console.log(a5);/ / the 1-2-3-4
console.log(arr);//['1', '2', '3', '4']
Copy the code

6. Push, add a data at the end of the array, and return the final length of the array

var a6=arr.push("789");    // Add a data at the end of the array and return the final length of the array.
console.log(a6);/ / 5
console.log(arr);// ['1', '2', '3', '4', '789']
Copy the code

7. Unshift, adding a data at the beginning of the array, returning the final length of the data

let arr = ['1'.'2'.'3'.'4']
var a7=arr.unshift("123");     // Add a data at the beginning of the array and return the final length of the data.
console.log(a7);/ / 5
console.log(arr);//['123', '1', '2', '3', '4']
Copy the code

8. Reverse, returns the array in reverse order, and the original array is reversed

let arr = ['1'.'2'.'3'.'4']
var a8=arr.reverse();       // Order the elements of the array in reverse order. Return the array in reverse order and the original array in reverse order.
console.log(a8);//['4', '3', '2', '1']
console.log(arr);//['4', '3', '2', '1']
Copy the code

9. The valueOf,toString, the general method of array is to print the contents of the array

console.log(arr.valueOf());       //['1', '2', '3', '4']
console.log(arr.toString());    / / 1, 2, 3, 4
Copy the code

10. Sort (), which sorts the elements of an array.

We will create an array and sort it alphabetically:var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"

document.write(arr + "<br />")
document.write(arr.sort())

George,John,Thomas,James,Adrew,Martin
Adrew,George,James,John,Martin,Thomas
/ / from w3school
Copy the code
We will create an array and sort it by number size:function sortNumber(a,b)
{
return a - b
}
var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort())

10.5.40.25.1000.1
1.5.10.25.40.1000
/ / from w3school
Copy the code

11. ForEach, loop through the array

carsList.forEach(item= > {
    for(var i=0; i<item.product_list.length; i++){let data ={
            user_id:localStorage.user_id,
            shop_cart_id:item.shop_cart_id,
            index:item.product_list[i].index
        }
        delinfo.push(data)
    }
})
Copy the code
The forEach() method is used to call each element of the array and pass the element to the callback function. Note: forEach() does not perform a callback function for an empty array.Copy the code

12. The map method returns a new array whose elements are the processed values of the original array elements.

The map() method returns a new array whose elements are the processed values of the original array elements. The map() method processes the elements in the original array element order. Note: Map () does not detect empty arrays. Note: Map () does not change the original array.Copy the code
const users=res.items.map(item= > ({
    url: item.html_url,      
    img: item.avatar_url,      
    name: item.login,
    })
);
Copy the code
let arr = ['1'.'2'.'3'.'4']
function sum(num){
  return num * 5
}
let a8 = arr.map(sum)
console.log(a8);//[5, 10, 15, 20]
Copy the code

13. The copyWithin method is used to copy elements from a specified position in an array to another specified position in the array.

Array. CopyWithin (Target, Start, End) Target Required. Copies to the specified target index location. Start the optional. The starting position of the element copy. The end is optional. The index location where replication is stopped (default is array.length). If it's negative, it's reciprocal.Copy the code
var fruits = ["1"."2"."3"."4"."5"."6"];
fruits.copyWithin(2.1.3);

1.2.2.3.5.6
Copy the code

The entries() method returns an iteration object for an array containing the array’s key/value pairs

let arra = ['a'.'b'.'c'.'d'];
arra.entries();

[0."a"]
[1."b"]
[2."c"]
[3."d"]

Copy the code

15. The every() method checks whether all the elements of the array meet the specified criteria (provided by the function).

The every() method checks whether all the elements of the array meet the specified criteria (provided by the function). The every() method uses the specified function to detect all elements in the array: if an element in the array is detected that does not satisfy, the entire expression returns false, and the remaining elements are not detected again. Returns true if all elements meet the criteria. Note: Every () does not check for empty arrays. Note: Every () does not change the original array.Copy the code
var ages = [32.33.16.40];
function checkAdult(age) {
    return age >= 18;
}
let flag = ages.every(checkAdult);
console.log(flag) //false
Copy the code

16. Fill (), which replaces the elements of an array with a fixed value.

Array. Fill (value, start, end) value Is required. Fill the value. Start the optional. Start filling the position. The end is optional. Stop filling position (default: array.length)Copy the code
let arra = ['a'.'b'.'c'.'d'];
arra.fill("a");
a,a,a,a
Copy the code
fruits.fill("e".2.4);
a,b,e,e
Copy the code

The 17.filter() method creates a new array by checking all the elements in the specified array that meet the criteria.

var ages = [32.33.16.40];
function checkAdult(age) {
    return age >= 18;
}
let arr = ages.filter(checkAdult);
console.log(arr) / / 32,33,40

let goodsList = this.goodsList.filter(item= >item.checked === true)
Copy the code

18.find(), which returns the value of the first element of the array tested.

The find() method returns the value of the first element of the array that has been tested. The find() method calls function execution once for each element in the array: when an element in the array returns true on a test condition, find() returns the elements that match the condition, and no subsequent values are called to the execution function. Return undefined if there are no matching elements. Note: find() does not execute for an empty array. Note: find() does not change the original value of the array.Copy the code
var ages = [32.33.16.40];
function checkAdult(age) {
    return age >= 18;
}
let arr = ages.find(checkAdult);
console.log(arr) / / 32
Copy the code

19. The findIndex() method returns the position of the first element in the array passed a test condition (function) that matches the condition.

The findIndex() method calls function execution once for each element in the array: when an element in the array returns true on a test condition, findIndex() returns the index position of the element that matches the condition, and subsequent values are not called to the execution function. Return -1 if there are no matching elements. Note: findIndex() does not execute for an empty array. Note: findIndex() does not change the original value of the array.Copy the code
var ages = [32.33.16.40];
function checkAdult(age) {
    return age >= 18;
}
let arr = ages.findIndex(checkAdult);
console.log(arr) / / 0
Copy the code

The.from() method is used to return an array from an object with a length property or an iterable.

fromThe () method is used to return an array from an object or iterable that has a length attribute. Returns if the object is an arraytrueOtherwise returnfalse.Array.from(object, mapFunction, thisValue) object Is an object that must be converted to an array. MapFunction Optional, the function to be called for each element in the array. ThisValue optional, in the mapFunctionthisObject.Copy the code
var setObj = new Set(["a"."b"."c"]);
var objArr = Array.from(setObj);
objArr[1] = ="b"; 
console.log(objArr[1] = ="b");
console.log(objArr);
//true
//[ 'a', 'b', 'c' ]
Copy the code

The.includes() method determines whether an array contains a specified value. It returns true if it does, false otherwise.

Arr.includes (searchElement) Arr.includes (searchElement, fromIndex) searchElement is required. The value of the element to look for. FromIndex optional. Start at this index to find the searchElement. If the value is negative, the search starts from the index of array.length + fromIndex in ascending order. The default is 0.Copy the code
[1.2.3].includes(2);     // true
[1.2.3].includes(4);     // false
[1.2.3].includes(3.3);  // false
[1.2.3].includes(3, -1); // true
[1.2.NaN].includes(NaN); // true
Copy the code

22. IndexOf () returns a specified element position in an array.

This method retrieves the array from beginning to end to see if it contains the corresponding element. The retrieval starts at the start of the array or at the beginning of the array (if no start argument is specified). If an item is found, the first occurrence of the item is returned. The index of the starting position is 0. Returns -1 if the specified element is not found in the array. Array.indexof (Item,start) TEM Yes. The element to look for. Start This parameter is an integer. Specifies the location in the array where the retrieval begins. Its legal values are 0 through stringobject.length - 1. If this parameter is omitted, the search starts at the first character of the string.Copy the code
var fruits=["Banana"."Orange"."Apple"."Mango"."Banana"."Orange"."Apple"];
var a = fruits.indexOf("Apple".4);
/ / 6
Copy the code

23. The isArray() method is used to determine whether an object is an array.

Returns if the object is an arraytrueOtherwise returnfalse.Array.isArray(obj)
Copy the code

The lastIndexOf() method returns the last occurrence of a specified element in the array, looking forward from the end of the string.

If the element to be retrieved does not appear, the method returns -1. This method retrieves the specified item element from end to end of the array. The retrieval starts at the start of the array or at the end of the array (if no start argument is specified). If an item is found, return item to retrieve the position of the first occurrence in the array from the end forward. The array index starts at0In the first place. Returns - if the specified element is not found in the array1. Array. LastIndexOf (Item,start) Item required. Specifies the string value to be retrieved. Start This parameter is an integer. Specifies the position in the string where the search begins. Its legal value is0To stringObject. Length -1. If this parameter is omitted, the retrieval starts at the last character of the string.Copy the code
var fruits=["Banana"."Orange"."Apple"."Mango"."Banana"."Orange"."Apple"];
var a = fruits.lastIndexOf("Apple");
/ / 6

var fruits=["Banana"."Orange"."Apple"."Mango"."Banana"."Orange"."Apple"];
var a = fruits.lastIndexOf("Apple".4);
/ / 2
Copy the code

25. The reduce() method receives a function as an accumulator, and each value in the array (from left to right) begins to shrink, eventually calculating to a single value.

Array. Reduce (Function (Total,currentValue, currentIndex, ARR), initialValue) Function (Total,currentValue, Index, ARR) Required. A function that executes each array element. Function parameters: Parameter description Total required. The initial value, or the value returned after the calculation. CurrentValue required. CurrentIndex is optional. The index ARR of the current element is optional. The array object to which the current element belongs. The initialValue is optional. The initial value passed to the functionCopy the code
var ages = [32.33.16.40];
function getSum(total, num) {
  return total + num;
}
let sum = ages.reduce(getSum);
console.log(sum); / / 121
Copy the code

ReduceRight () method function andreduce()The function is the same. The difference isreduceRight()Adds the items in an array from the end forward.

array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
function(total,currentValue, index,arr)
totalA necessity. The initial value, or the value returned after the calculation.currentValueA necessity. The current elementcurrentIndexOptional. Index of the current elementarrOptional. The array object to which the current element belongs.initialValueOptional. The initial value passed to the functionCopy the code

The slice() method returns the selected elements from an existing array.

The //slice() method extracts a part of the string and returns the extracted part as a new string
// Note: the slice() method does not change the original array.
var fruits = ["Banana"."Orange"."Lemon"."Apple"."Mango"];
var citrus = fruits.slice(1.3);
//Orange,Lemon
// Return a new array containing the elements of arrayObject from start to end (excluding that element).
Copy the code
Parameter Description start This parameter is optional. Specifies where to start selecting. If it's negative, then it specifies the position from the end of the array. That is, -1 is the last element, -2 is the penultimate element, and so on. The end is optional. Specifies where to end the selection. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the shred array contains all elements from start to the end of the array. If this parameter is negative, then it specifies that the elements are counted from the end of the array.Copy the code

28. The some() method checks whether the elements in the array meet the specified conditions (provided by the function)

The some() method checks whether the elements in the array meet the specified conditions (provided by the function). The some() method executes each element of the array in turn: if one of the elements satisfies the condition, the expression returns true, and the remaining elements are no longer tested. If there are no elements that satisfy the condition, false is returned. Note: some() does not detect empty arrays. Note: some() does not change the original array.Copy the code
var ages = [3.10.18.20];

function checkAdult(age) {
    return age >= 18;
}
let arr = ages.some(checkAdult);
//true
Copy the code
Parameter Description function(currentValue, index, ARR) Yes. Function, which is executed for each element in the array. The value of the current element index is optional. The index value of the current element arR Is optional. ThisValue, the array object to which the current element belongs, is optional. Object used as the callback for this execution, passed to the function as the value of "this". If thisValue is omitted, the value of "this" is" undefined"Copy the code