Introduction to JavaScript Array objects
1. An introduction to arrays is an ordered set of values. Each value is called an element, and each element has a position in the array, which is represented by numbers and is called an index. JavaScript arrays are non-typed: array elements can be of any type, and different elements in the same Array may have different types. -- JavaScript authoritative guide (Sixth Edition) 2. define var names = new Array ("Zhang San", "Li Si", "Wang Wu"); // or var names = ["Zhang San", "Li Si", "Wang Wu"]; 3. attribute length: the length of elements in the array. 4. common examples: 1) unshift (): insert an element in the array header 2) shift (): Remove and return the first element of the array 3) push (): insert element 4 at the end of the array) pop (): Remove and return the last element 4.1 concat () of the array: concatenate the element into the array. The original array is not modified, and the new array parameters are returned: ① value1, value2 ..... valueN: return value of any number of values: {Array} a new Array containing the original Array and newly added elements. Example: var demoArray = ['A', 'B', 'C']; var demoArray2 = demoArray. concat ('E'); console. log (demoArray); // => demoArray: ['A', 'B', 'C'] the original array does not change the console. log (demoArray2); // => ['A', 'B', 'C', 'E'] 4.2 every (): traverse elements in sequence, determine whether each element is a true parameter: ① function (value, index, self) {}: each element uses this function to determine whether it is true. When it is determined that one is false, terminate the traversal immediately. Value: index of the element traversed by the Array: Element No. self: Array returned value: {Boolean}: true is returned only when each element is true. If one element is false, false is returned. Example: var demoArray = [1, 2, 3]; var rs = demoArray. every (function (value, index, self) {return value> 0 ;}); console. log (rs); // => true 4.3 filter (): traverses elements in sequence and returns a new array containing elements that meet the conditions. Parameter: ① function (value, index, self) {}: each element calls this function in sequence and returns a new array containing the element that meets the condition. Value: index of elements traversed by an Array: Element No. self: Array returned value: {Array} a new Array containing elements that meet the conditions: var demoArray = [1, 2, 3]; var rs = demoArray. filter (function (value, index, self) {return value> 0 ;}); console. log (rs); // => [1, 2, 3] 4.4 forEach (): traverses elements in sequence and executes the specified function; no return value. Parameter: ① function (value, index, self) {}: each element calls this function in turn value: index of the element traversed by the Array: Element No. self: Array return value: no example: var demoArray = [1, 2, 3]; demoArray. forEach (function (value, index, self) {console. log (value); // => output in sequence: 1 2 3}); 4.5 indexOf (): searches for matching elements in the array. If no matching element exists,-1 is returned. When searching, the "=" operator is used. Therefore, we need to distinguish between 1 and '1' parameters: ① value: the value to be searched in the array. ② Start: the position of the sequence number to start searching. If it is omitted, It is 0. return value: {Int}: returns the sequence number of the first matched value in the array. If no value exists, return-1 Example: ['A', 'B', 'C']. indexOf ('A'); // => 0 ['A', 'B', 'C']. indexOf ('A', 1); // =>-1 ['A', 'B', 'C']. indexOf ('D'); // =>-1 [1, 2, 3]. indexOf ('1'); // =>-1: Use the '=' matching method 4.6 join (): concatenates all elements in the array into a string using a separator. Parameter: ① sparator {String}: delimiter between each element. If omitted, the separator is separated by commas (,) by default. Return Value: {String}: A String concatenated by sparator. Example: ['A', 'B', 'C']. join (); // => 'a, B, C' ['A', 'B', 'C']. join ('-'); // => 'a-B-C' 4.7 lastIndexOf: reverse searches for matching elements in the array. If no matching element exists,-1 is returned. When searching, the "=" operator is used. Therefore, we need to distinguish between 1 and '1' parameters: ① value: the value to be searched in the array. ② Start: the position of the sequence number to start searching. If this parameter is omitted, the sequence number is searched from the last element. Return value: {Int}: searches for the sequence number of the first matching value in the array from right to left. If no value exists, return-1 Example: ['A', 'B ', 'C']. lastIndexOf ('A'); // => 0 ['A', 'B', 'C']. lastIndexOf ('A', 1); // => 0 ['A', 'B', 'C']. lastIndexOf ('D'); // =>-1 [1, 2, 3]. lastIndexOf ('1'); // =>-1: map () using the '=' matching method: traverse and compute each element in sequence, return the array parameter of the calculated element: ① function (value, index, self) {}: each element calls this function in turn. Return the calculated element value: index of the element traversed by the array: element No. self: Array returned value: {Array} a new Array example containing even better elements: [1, 2, 3]. m Ap (function (value, index, self) {return value * 2 ;}); // => [2, 4, 6] 4.9 pop (): remove and return the last element parameter of the array: No returned value: the last element of the {Object} array. If the array is empty, return the undefined example: var demoArray = ['A ', 'B', 'C']; demoArray. pop (); // => cdemoArray. pop (); // => bdemoArray. pop (); // => ademoArray. pop (); // => undefined 4.10 push (): add the element 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 the array. Example: var demoArray = ['A', 'B ', 'C']; demoArray. push ('D'); // => 4, demoArray: ['A', 'B', 'C', 'D'] demoArray. push ('E', 'F'); // => 6, demoArray: ['A', 'B', 'C', 'D', 'E ', 'F'] console. log (demoArray); // => ['A', 'B', 'C', 'D ', 'E', 'F'] 4.11 reverse (): returns the order of array elements. Parameter: no return value: none (reverse element order in the original number group ). Example: var demoArray = ['A', 'B', 'C', 'D', 'E']; demoArray. reverse (); console. log (demoArray); // => ["e", "d", "c", "B", "a"] 4.12 shift (): remove and return the first element parameter of the array: no return value: the first element of the {Object} array; if the array is empty, return undefined. Example: var demoArray = ['A', 'B', 'C']; demoArray. shift (); // => ademoArray. shift (); // => bdemoArray. shift (); // => cdemoArray. shift (); // => undefined 4.13 slice (startIndex, endIndex): return part of the array. Parameter: ① startIndex: sequence number at the beginning. If it is a negative number, it indicates that the calculation starts from the end,-1 indicates the last element,-2 indicates the second to the last, and so on. ② EndIndex: The last sequence number of the end element. If it is not specified, it is the end. The intercepted element does not contain the element of the sequence number. It ends with the first element of the sequence number. Returned value: {Array} a new Array containing all elements of the previous element from startIndex to endIndex. Example: [1, 2, 3, 4, 5, 6]. slice (); // => [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]. slice (1); // => [2, 3, 4, 5, 6]: [1, 2, 3, 4, 5, 6] Starting from number 1. slice (0, 4); // => [1, 2, 3, 4]: intercept the element [1, 2, 3, 4, 5, 6]. slice (-2); // => [5, 6]: extract the following two elements 4.14 sort (opt_orderFunc): sort parameters according to certain rules: ① opt_orderFunc (v1, v2) {Function}: an optional sorting rule Function. If omitted, the elements are sorted in ascending order by letters. V1: the element before the traversal. V2: elements after the time. Sort rules: Compare v1 and v2. A number is returned to indicate the v1 and v2 sort rules: smaller than 0: v1 is smaller than v2, and v1 is placed before v2. Equal to 0: v1 is equal to v2, and v1 is placed before v2. Greater than 0: v1 is greater than v2, and v1 is placed behind v2. Returned value: none (sorting in the original array ). Example: [1, 3, 5, 2, 4, 11, 22]. sort (); // => [1, 11, 2, 22, 3, 4, 5]: All elements are converted to characters, the characters before 11 are [1, 3, 5, 2, 4, 11, 22]. sort (function (v1, v2) {return v1-v2;}); // => [1, 2, 3, 4, 5, 11, 22]: sort [1, 3, 5, 2, 4, 11, 22] in ascending order. sort (function (v1, v2) {return-(v1-v2); // you can convert it to large to small}); // => [22, 11, 5, 4, 3, 2, 1] 4.15 splice (): insert or delete an array element parameter: ① start {int}: start sequence number for starting insertion, deletion, or replacement. ② DeleteCount {int}: number of elements to be deleted, which is calculated from start. ③ Value1, value2... valueN {Object}: an optional parameter that indicates the element to be inserted and is inserted from start. If the ② parameter is not 0, the delete operation is performed first, and then the insert operation is performed. Returned value: {Array} returns a new Array containing the deleted elements. If the ② parameter is 0, no elements are deleted, and an empty array is returned. Example: // 1. delete var demoArray = ['A', 'B', 'C', 'D', 'E']; var demoArray2 = demoArray. splice (0, 2); // Delete two elements starting from 0 and return an array containing the deleted elements: ['A', 'B'] console. log (demoArray2); // => ['A', 'B'] console. log (demoArray); // => ['C', 'D', 'E'] // 2. insert var demoArray = ['A', 'B', 'C', 'D', 'E']; var demoArray2 = demoArray. splice (0, 0, '1', '2', '3'); // If the parameter is 0, an empty array console is returned. log (demoArray2); // => [] console. log (demoArray );// => ['1', '2', '3', 'A', 'B', 'C', 'D', 'E'] // 3. first Delete and then insert var demoArray = ['A', 'B', 'C', 'D', 'E']; // when parameter ② is not 0, then, execute the delete operation (delete the four elements whose sequence number starts from 0 and return an array containing the deleted elements), and then execute the insert operation var demoArray2 = demoArray. splice (0, 4, '1', '2', '3'); console. log (demoArray2); // => ['A', 'B', 'C', 'D'] console. log (demoArray); // => ['1', '2', '3', 'A', 'B', 'C', 'D ', 'E'] 4.16 toString (): concatenates all elements in the array with a comma (,) into a string. Parameter: no return value: all elements in the {String} array are concatenated into a String with a comma (,) and returned. It is the same as calling the join () method without parameters. Example: [1, 2, 3, 4, 5]. toString (); // => '1, 2, 3, 4, 5 '['A',' B ', 'C', 'D', 'E']. toString (); // => 'a, B, c, d, e' 4.17 unshift (): insert element parameters in the array header: ① value1, value2 ..... valueN: add any number of values to the array header return value: {int} new length of the array example: var demoArray = []; demoArray. unshift ('A'); // => demoArray: ['a'] demoArray. unshift ('B'); // => demoArray: ['B', 'a'] demoArray. unshift ('C'); // => demoArray: ['C', 'B', 'a'] demoArray. unshift ('D'); // => demoA Rray: ['D', 'C', 'B', 'a'] demoArray. unshift ('E'); // => demoArray: ['E', 'D', 'C', 'B', 'a'] 5. static Method 5.1 Array. isArray (): determines whether the Object is an array parameter: ① value {Object}: Any Object return value: {Boolean} returns the judgment result. If it is true, it indicates that the object is an Array; if it is false, it indicates that the object is not an Array example: Array. isArray ([]); // => trueArray. isArray (['A', 'B', 'C']); // => trueArray. isArray ('A'); // => falseArray. isArray ('[1, 2, 3]'); // => false 6. actual Operation 6.1 index Description: Each element has a position in the array, expressed as a number, which is called an index. The index starts from 0, that is, the index of the first element is 0, and the index of the second element is 1. Therefore, when an index that does not exist in the array is obtained, undefined is returned. Example: var demoArray = ['A', 'B', 'C', 'D', 'E']; demoArray [0]; // => obtain the first element: 'A' demoArray [0] = 1; // set the first element to 1console. log (demoArray); // => demoArray: [1, 'B', 'C', 'D', 'E'] console. log (demoArray [9]); // => undefined: If the obtained index does not exist, the undefined 6.2 for statement is returned. Description: You can use the for statement to traverse the array one by one. Example: var demoArray = ['A', 'B', 'C', 'D', 'E']; for (var I = 0, length = demoArray. length; I <length; I ++) {console. log (demoArray [I] ); // => Elements in the output Array one by one} 6.3 note: the Array type is a reference type. When Array a is copied to Array B, modify the element of array B, and array a also changes. Example: var demoArrayA = ['A', 'B', 'C', 'D', 'E']; var demoArrayB = demoArrayA; // assign array A to array BdemoArrayB [0] = 1; // modify the elements of array B on the console. log (demoArrayA); // => [1, 'B', 'C', 'D', 'E']: the element of array A has also changed. 6.4 deep copy Description: The concat () method is used to return A new array. To prevent the occurrence of shallow copy, modify the element of array B, array a does not change. Example: var demoArrayA = ['A', 'B', 'C', 'D', 'E']; var demoArrayB = demoArrayA. concat (); // use the concat () method to return the new array demoArrayB [0] = 1; // modify the elements of array B on the console. log (demoArrayA); // => ['A', 'B', 'C', 'D', 'E']: The elements of array a have not changed the console. log (demoArrayB); // => [1, 'B', 'C', 'D', 'E']: Elements of array B have changed.