JS array method

How to create an array

1. Literal form: var arr=;

1. Constructor: var arr1=new Array();/ /Uncommonly used

The Array constructor has a big flaw, that is, different parameters will cause its behavior to be inconsistent.

1. A single value is used as a parameter, and the parameter represents the number of elements in the array.

It can be seen that Array, as a constructor, behaves very inconsistently. Therefore, it is not recommended to use it to generate new arrays, and it is better to use array literals directly.

push/pop/unshift/shift//Add and delete elements (array method, so you should call it when using

array name. method name ())

< p> arr. slice/splice//Intercept elements

arr.slice(); The original array does not change

When there are no parameters, the original array is returned, which is equivalent to copying the array.

When there is one parameter, intercept the element subscripted by the parameter to the end of the array.

When there are two parameters, interception starts from the element with the first parameter as the subscript (index) and ends with the element with the second parameter as the subscript, but does not include the function with the second parameter as the subscript. . (The starting element is included, the ending element is not included)

When there are multiple parameters, the first two parameters are valid, and the following parameters are invalid.

arr.splice(); changes the original array

When there are no parameters, an empty array is returned.

When there is one parameter, it is intercepted from the element subscripted by the parameter to the end of the array.

When there are two parameters, interception starts from the element with the first parameter as the subscript (index), which indicates the starting position of interception, and the second parameter indicates the number of intercepted elements.

When there are multiple parameters, the first two parameters take effect, and the following parameters are filled from the starting position of the original array to the original array.

reverse/sort//Change element position

arr.reverse(); //Array flip (element position reversed)

arr.sort(); Sorting from small to large, but following the bitwise comparison rules of strings, so the sorting results are prone to exceptions.

join();//Does not change the original array

join() uses the specified parameter as the connector to connect all array members into a string and return it. If no parameters are provided, they default to commas.

concat();//Splicing arrays does not change the original array

ES5 new array operation method

indexOf (item) returns the corresponding element in the array Index value, if not found, returns -1 to test whether the element exists in the array

forEach(function(item,index)) traverses the array, no return value

map(function(item,index)) traverses the array, there is a return value

filter(function(item)) {return item>2} returns elements greater than 2

some returns Boolean value, the condition is partially true || arr.some(function(item){return item>2} )

every returns a Boolean value, the condition is all true&& arr.every(function(item){return item> 2} )

reduce (Invoke the specified callback function on all elements in the array. The return value of the callback function is the cumulative result, and this return value is provided as a parameter the next time the callback function is called.) arr.reduce(function(a,b){return a+b;});

toString() and toLocaleString()

Function: Convert each element of the array to string, and output a comma-separated list of strings.

The function is similar to join();

Parameters: None

Output: String

indexOf() and lastIndexOf()

Function: Searches the entire array for an element with a given value, returning the index of the first element found or -1 if not found; lastIndexOf is a reverse search.

Parameters: element value, starting index (optional)

Output: index value or -1

Array.from()

< p> Function: Convert two types of objects into real arrays: array-like objects and traversable objects

Parameters: The object to be converted, the second parameter is optional, and its function is similar to the map of the array Method, used to process each element and put the processed value into the returned array.

Output: Array

Array.of()

Function: Convert a set of values ??into an array.

Parameter: Array element

Output: Array

copyWithin()

Function: Inside the current array, copy the member at the specified position Copy to other location and return the changed array.

Parameters: index (replace data from this position); index (start reading data from this position, default is 0, negative value indicates reciprocal); index (stop reading before reaching this position, default is the maximum index)

Output: Returns the current replaced array.

Note: Changed the current array

find() and findIndex()

Function: Find the first array member that meets the conditions.

Parameters: callback function, all array members execute this function in sequence until the first member with a return value of true is found. The callback function can accept three parameters, which are value, position and original array.

Output: find() returns the found member; findIndex() returns the position of the member.

fill()

Function: Fill an array with the given value.

Parameters: The first parameter is the value to be filled, the second and third parameters are optional, indicating the starting and ending positions of filling respectively (not included).

Output: filled array

entries(), keys(), values()

Function: used to traverse the array, you can use for...of Loop through. The difference is that keys() traverses key names, values ????traverses key values, and entries() traverses key-value pairs.

Parameters: None

Output: Traverser object

includes()

Function: Indicates whether an array contains a given Value

Parameters: The first parameter is required (the given value to be checked), the second parameter is optional and represents the starting position of the search. The default is 0, and a negative number represents the reciprocal position.

Output: A Boolean value.

Note: The difference from indexOf is that indexOf performs a strong comparison of operators, which may lead to misjudgment of NaN.