Home >Web Front-end >JS Tutorial >A brief discussion on JavaScript Array objects_Basic knowledge
Array array
1. Introduction
An array is an ordered collection of values. Each value is called an element, and each element has a position in the array, represented by a number, called an index. JavaScript arrays are untyped: array elements can be of any type, and different elements in the same array may have different types. --"The Definitive Guide to JavaScript (6th Edition)"
2. Definition
3. Attributes
length: Indicates the length of elements in the array.
4. Instance methods
Common methods:
1) unshift(): Insert elements at the head of the array
2) shift(): Remove and return the first element of the array
3) push(): Insert elements at the end of the array
4) pop(): Remove and return the last element of the array
4.1 concat(): Connect elements to an array. The original array will not be modified and a new array will be returned
Parameters:
①value1,value2....valueN: any number of values
Return value:
{Array} A new array, containing the original Array and the newly added elements.
Example:
4.2 every(): Traverse the elements in sequence and determine whether each element is true
Parameters:
①function(value,index,self){}: Each element will use this function to determine whether it is true. When one is determined to be false, the traversal will end immediately.
Value: elements of array traversal
Index: element number
Self: Array itself
Return value:
{Boolean}: Returns true only if every element is true; returns false as long as one element is false.
Example:
4.3 filter(): Traverse the elements in sequence and return a new array containing elements that meet the conditions.
Parameters:
①function(value,index,self){}: Call this function on each element in turn and return a new array containing elements that meet the conditions.
Value: elements of array traversal
Index: element number
Self: Array itself
Return value:
{Array} A new array containing elements that meet the criteria
Example:
4.4 forEach(): Traverse the elements in sequence and execute the specified function; no return value.
Parameters:
①function(value,index,self){}: Call this function for each element in turn
Value: elements of array traversal
Index: element number
Self: Array itself
Return value: None
Example:
4.5 indexOf(): Find matching elements in the array. If there is no matching element, -1 is returned. Use the "===" operator when searching, so you need to distinguish between 1 and '1'
Parameters:
①value: The value to be found in the array.
②start: The serial number position to start searching. If omitted, it will be 0.
Return value:
{Int}: Returns the serial number of the first matching value in the array. If it does not exist, returns -1
Example:
4.6 join(): Splice all elements in the array into a string using a separator.
Parameters:
①sparator {String}: The separator between each element. If omitted, it will be separated by English commas ',' by default.
Return value:
{String}: Each element is spliced into a string with sparator as the separator.
Example:
4.7 lastIndexOf: Find matching elements in the reverse array. If there is no matching element, -1 is returned. Use the "===" operator when searching, so you need to distinguish between 1 and '1'
Parameters:
①value: The value to be found in the array.
②start: The serial number position to start searching. If omitted, the search will start from the last element.
Return value:
{Int}: Find the sequence number of the first matching value in the array from right to left. If it does not exist, return -1
Example:
4.8 map(): Traverse and calculate each element in sequence, and return an array of calculated elements
Parameters:
①function(value,index,self){}: Each element calls this function in turn and returns the calculated element
Value: elements of array traversal
Index: element number
Self: Array itself
Return value:
{Array} A new array containing the good elements
Example:
4.9 pop(): Remove and return the last element of the array
Parameters: None
Return value:
{Object} The last element of the array; if the array is empty, undefined
is returnedExample:
4.10 push(): Add elements to the end of the array
Parameters:
①value1,value2....valueN: Add any number of values to the end of the array
Return value:
{int} new length of array
Example:
4.11 reverse(): Reverse the order of array elements.
Parameters: None
Return value: None (reverse the order of elements in the original array).
Example:
4.12 shift(): Remove and return the first element of the array
Parameters: None
Return value:
{Object} The first element of the array; if the array is empty, undefined is returned.
Example:
4.13 slice(startIndex,endIndex): Return a part of the array.
Parameters:
①startIndex: The serial number at the beginning; if it is a negative number, it means counting from the end, -1 represents the last element, -2 represents the second to last element, and so on.
②endIndex: The serial number after the element at the end. If not specified, it is the end. The intercepted element does not contain the element with the serial number here, and ends with the element before the serial number here.
Return value:
{Array} A new array containing all elements from startIndex to the previous element of endIndex.
Example:
4.14 sort(opt_orderFunc): Sort according to certain rules
Parameters:
①opt_orderFunc(v1,v2) {Function}: Optional sorting function. If omitted, the elements will be sorted alphabetically from small to large.
v1: The previous element when traversing.
v2: The following elements when traversing.
Sort:
Compare v1 and v2 and return a number to represent the sorting rules of v1 and v2:
Less than 0: v1 is smaller than v2, v1 is ranked in front of v2.
Equal to 0: v1 is equal to v2, v1 is ranked in front of v2.
Greater than 0: v1 is greater than v2, v1 is ranked behind v2.
Return value: None (sort operation in the original array).
Example:
4.15 splice(): Insert and delete array elements
Parameters:
①start {int}: The starting sequence number to start inserting, deleting or replacing.
②deleteCount {int}: The number of elements to be deleted, counting from start.
③value1,value2 ... valueN {Object}: Optional parameter, indicating the element to be inserted, starting from start. If the ② parameter is not 0, then the deletion operation is performed first, and then the insertion operation is performed.
Return value:
{Array} Returns a new array containing the deleted elements. If the ② parameter is 0, it means that no elements are deleted and an empty array is returned.
Example:
4.16 toString(): Concatenate all elements in the array into a string through an English comma ','.
Parameters: None
Return value:
{String} All elements in the array are concatenated into a string through an English comma ',' and returned. The same as calling the join() method without parameters.
Example:
4.17 unshift(): Insert elements at the head of the array
Parameters:
①value1,value2....valueN: Add any number of values to the head of the array
Return value:
{int} new length of array
Example:
5. Static method
5.1 Array.isArray(): Determine whether the object is an array
Parameters:
①value {Object}: any object
Return value:
{Boolean} Returns the judgment result. When it is true, it means that the object is an array; when it is false, it means that the object is not an array
Example:
6. Practical operation
6.1 Index
Description: Each element has a position in the array, represented by a number, called an index. The index starts from 0, that is, the index of the first element is 0, the index of the second element is 1, and so on;
When getting an index that does not exist in the array, undefined is returned.
Example:
6.2 for statement
Note: You can traverse the array one by one through the for statement
Example:
6.3 Shallow Copy
Note: The Array type is a reference type; when array a is copied to array b, if the elements of array b are modified, array a will also be modified.
Example:
6.4 Deep Copy
Description: Use the concat() method to return a new array; to prevent shallow copying, modify the elements of array b, and array a will not change.
Example: