The Array object

Generate instance objects

var a = new Array(a)Copy the code

attribute

a.length  / / the length
Copy the code

judge

Array.isArray(a)  // true / false
Copy the code

Used to determine whether a value is an array

methods

value

Array.valueof()
Copy the code

Return the array itself

Array.toString()
Copy the code
Returns the string form of an arrayCopy the code

The enumeration

Keys (), values(), entries()Copy the code

conversion

Array.from(object, mapFunction, thisValue);

var setObj = new Set(["a"."b"."c"]);
var objArr = Array.from(setObj);
objArr[1] = ="b";  // true


var arr = Array.from([1.2.3].x= > x * 10);
// arr[0] == 10;
// arr[1] == 20;
// arr[2] == 30;
Copy the code

The method is to convert array-like objects (that is, objects with the Length attribute) and traversable objects into true arrays.

parameter describe
object Required, object to be converted to array.
mapFunction Optionally, the function to call for each element in the array.
thisValue Optionally, map this object in mapFunction.
Array.of();

let arr1 = Array.of(1.2.3); / / [1, 2, 3]
Copy the code

The method is to convert a set of values into an array. If the number of arguments is 0, an empty array is returned

Add or delete

Array.push(1.2)
Copy the code

Adds one or more elements to the end of the array and returns the new array length

Array.pop()
Copy the code

Removes and returns the last element of the array

Array.unshift(1.2)
Copy the code

Adds one or more elements to the beginning of an array and returns the new array length

Array.shift()
Copy the code

Removes the first item of the array and returns the value of the first element

Splicing/interception

Array.concat(arr1,arr2...)
Copy the code

Combine two or more arrays to generate a new array. The original array remains the same.

Array.join(",")
Copy the code

Concatenates each item of an array with the specified character to form a string

Array.slice(start , end)
Copy the code

Returns a new array containing elements in the ARR from start to end (excluding the element)

parameter describe
start Optional. Specify where to start. If it is negative, it specifies the position from the end of the array. If this parameter is negative, it extracts from the penultimate element of the original array, and slice(-2) extracts from the penultimate element of the original array to the last element, including the last element.
end Optional. Specify where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the shard array contains all elements from start to the end of the array. If this parameter is negative, it indicates the penultimate element in the original array at the end of the extraction. Slice (-2,-1) extracts the penultimate element of the array to the last element (excluding the last element, i.e. only the penultimate element).
arr.splice(index , howmany , item1,..... ,itemX)Copy the code

Add/remove items to/from the array and return the array of deleted items

Index: required. Integer to specify where items are added/removed, and negative numbers to specify positions from the end of the array.

Howmany: Required. Number of items to delete. If set to 0, the project will not be deleted.

item1, … , itemX: Optional. The new item added to the array.

The sorting

Array.reverse()
Copy the code

Invert the array. The original array changes.

Array.sort();

// From small to big
arr.sort((a,b) = >{
    return a-b
})
//② From large to small
arr.sort((a,b) = >{
    return b-a
})
Copy the code

Sort array elements

The query

indexOf(element,start)
Copy the code

Returns the position of the given element’s first occurrence in the array, or -1 if none occurs. You can accept a second argument that indicates the start of the search

lastIndexOf(element)
Copy the code
Returns the last occurrence of the given element in the array, or -1 if none occurred.Copy the code
find();

let Arr = [1.2.5.7.5.9];
let result1 = Arr.find(function(currentValue,index,arr){			
    return currentValue>5;
});
console.log(result1); / / 7
Copy the code

Method returns the value of the first element of the array that passes the test.

findIndex();
Copy the code

FindIndex is similar to find, except that it returns the index by default, or -1 if there is no element that matches the criteria

array.fill(value,  start,  end);
Copy the code

The fill() method fills all elements of an array from the start index to the end index with a fixed value. Does not include terminating indexes.

arr.includes(searchElement, fromIndex);

[1.2.3].includes(2);
Copy the code
parameter describe
searchElement Must be. The value of the element to look up.
fromIndex Optional. The search for searchElement starts at that index. If it is negative, the search starts from the index of array.length + fromIndex in ascending order. The default is 0.

An iterative approach

//5 iterative methods:ForEach (), map(), filter(), some(), every()// These methods have the same syntax and do not change the original array.
currentValue: required. Current element index: optional. The index value of the current element.arr: optional. The array object to which the current element belongs.ForEach () : Iterates through the array. This method returns no value.
Arr.forEach(function(currentValue, index, arr){
    console.log(index+"--"+currentValue+"--"+(arr === Arr));		
})

//map() : refers to "mapping", the method returns a new array
var arr2 = arr.map(function(currentValue){
    return currentValue*currentValue;
});

//filter() : The "filter" function creates a new array
var result1 = arr.filter(function(currentValue){
    return currentValue>5;
});

//every() : checks whether every item in the array meets the condition. Only all items meet the condition returns true.
var arr = [1.4.6.8.10];
var result1 = arr.every(function(currentValue){
 return currentValue< 12;
});
console.log(result1);  // true

//some() : checks whether there are any items in the array that satisfy the condition, and returns true as long as one item satisfies the condition.
var arr = [1.4.6.8.10];
var result1 = arr.some(function(currentValue){
     return currentValue> 10;
});
console.log(result1);  // false
Copy the code

Merge method

Reduce () and reduceRight ()Copy the code

Method starts with the first item in the array and iterates to the end. ReduceRight (), on the other hand, starts from the last item of the array and traverses forward to the first item.

Reduce () syntax:

arr.reduce(function(total , cur , index , arr){//do something}, initialValue)
Copy the code

ReduceRight () syntax:

arr.reduceRight(function(total , cur , index , arr){//do something}, initialValue)
Copy the code

Total: required. The initial value, or the return value at the end of the calculation. Cur: Necessary. Current element. Index: Optional. The index of the current element. Arr: Optional. The array object to which the current element belongs. InitialValue: Optional. The initial value passed to the function.

var result2 = arr.reduce(function(total,cur,index,arr){	
    console.log("total:"+total+",cur:"+cur+",index:"+index);
 return total+cur;
},10);
console.log("Result:"+result2);
/ / output
// total:10,cur:1,index:0
// total:11,cur:2,index:1
// total:13,cur:3,index:2
// total:16,cur:4,index:3
// total:20,cur:5,index:4
// Result: 25
Copy the code