What is an array?
Array: a window that stores a set or series of related data, equivalent to a combination of multiple variables.
- When multiple data are stored and processed, use an array. What if 5 or 50 data are stored?
- Array is a continuous space in memory. Compared with variables, it has higher and faster performance for storing or reading data
Array subscript: the index number that can identify a unique array space.
Subscript starts from 0, maximum subscript: length of array - 1
Element of array: the data stored in the array with unique index numbers is called element.
How do I declare arrays?
- Literal method: []
- Constructor method: new Array()
var arr = []; //Declare an empty array var list = new Array(); //Declare an empty array
The difference between the two declaration methods?
- In literal mode, no matter what data is stored, it represents array elements.
- When there is only one positive integer in the constructor, it indicates the length of the array.
- When there is a negative integer or decimal in the constructor, an error is reported directly
- Represents an array element when there is one or more other types of data in the constructor.
var arr = [5]; // Indicates that one element in the array is 5 and the length is 1 var arr = new Array(5); //Indicates that the length of this array is 5 and can hold 5 elements. The default element is undefined,undefined,undefined,undefined,undefined var arr = new Array(-5); //The length cannot be negative. The program reports an error var arr = new Array(5.3); //The length cannot be decimal. The program reports an error var arr = new Array('3'); //Indicates that an element in the array is'3',The length is 1 var arr = new Array(1,2,3,4,5);//Indicates that there are 5 elements in the array, 1 2 3 4 5 respectively ,The length is 5
How do I access arrays?
Array name [subscript]
var arr = [1,2,3,4,5]; //Declare an array containing five elements arr[5] = 6; //Added an element 6 to the array with a subscript of 5 console.log(arr[0]); //The element corresponding to the 0 subscript in the access array is 1
Array properties
length : Indicates the length of the array (that is, how much memory space is in the array)
var arr = [1,2,3,4]; console.log(arr.length); // 4 Represents the length of the array
Array method (add, delete, modify, cut, spell, compound, arrange, transfer)
Add:
Unshift (element, element, element,...)
- Function: add a new element to the head of the array.
- Return value: the length of the array after adding
- Affect original array: Yes
var arr = [5,6,7,8,9]; //Declare an array console.log(arr.unshift(true,[1,2,3],false)); //unshift Method, 8 means that the length of the array after adding is 8 console.log(arr); // Output original array [true,[1,2,3],false,5,6,7,8,9]
Push (element, element, element,...)
- Function: add new elements at the end of the array.
- Return value: the length of the array after adding
- Affect original array: Yes
var arr = [5,6,7,8,9]; console.log(arr.push(true,[1,2,3],false)); //push Method, 8 means that the length of the array after adding is 8 console.log(arr); // Output original array [5, 6, 7, 8, 9, true, [1,2,3], false]
Delete:
shift()
- Function: delete an element at the head of the array (only one element can be deleted at a time)
- Return value: deleted element
- Affect original array: Yes
var arr = [5,6,7,8,9]; console.log(arr.shift()); //shift Method, 5 represents the element 5 deleted in the array console.log(arr); // Output original array [6, 7, 8, 9] //Delete all elements var arr = [5,6,0,8,9]; while(arr.length){ arr.shift(); } console.log(arr);
pop()
- Function: delete an element at the end of the array (only one element can be deleted at a time)
- Return value: deleted element
- Affect original array: Yes
var arr = [5,6,7,8,9]; console.log(arr.pop()); //9 pop Method, 9 represents the element 9 deleted from the array console.log(arr); // Output original array [5, 6, 7, 8]
Change:
Splice (start, del_, length, element, element,...)
- start: from which subscript position
- del_length: delete several elements
- Element: new element
- Function: you can add, delete and change at any position of the array.
- Return value: array of deleted elements
- Affect original array: Yes
var arr = [5,6,7,8,9]; //When there is only one parameter, it means to delete from the specified subscript to the end of the array console.log(arr.splice(1)); // [6, 7, 8, 9] Return value console.log(arr); // [5] Original array var arr1 = [5,6,7,8,9]; //With two parameters, it means that the element of the specified length is deleted from the specified subscript position console.log(arr1.splice(1,2)); // [6, 7] console.log(arr1); // [5, 8, 9] var arr2 = [5,6,7,8,9]; //Add a new element at the deleted location. Delete 2 elements from subscript 1 and add new elements at the position of subscript 1 true,false Two elements console.log(arr2.splice(1,2,true,false)); // [6, 7] console.log(arr2); // [5, true, false, 8, 9] var arr3 = [5,6,7,8,9]; //Delete 0 elements at subscript 1 and add two elements at subscript 1 true,false console.log(arr3.splice(1,0,true,false)); // [] console.log(arr3); // [5, true, false, 6, 7, 8, 9]
Cut:
slice(start,end)
- start: from which subscript position
- End: to which subscript position to end (excluding the end position)
- Function: intercept the elements of the specified range in the array
- Return value: the intercepted element array
- Affect original array: no
var arr = [5,6,7,8,9]; // Intercept from the subscript 1 position in the original array to the end of the array, and return the intercepted new array console.log(arr.slice(1)); // [6, 7, 8, 9] Return value console.log(arr); // [5,6,7,8,9] Original array var arr1 = [5,6,7,8,9]; //Intercept from the position of subscript 1 in the original array to before subscript 3 (excluding subscript 3) console.log(arr1.slice(1,3)); // [6, 7] Return value console.log(arr1); // [5,6,7,8,9] Original array var arr2 = [5,6,7,8,9]; //Intercept from the subscript 3 position in the original array to before the subscript 1 (because it can only be intercepted to the right, all elements cannot be intercepted) console.log(arr2.slice(3,1)); // [] Return value console.log(arr2); // [5,6,7,8,9] Original array var arr3 = [5,6,7,8,9]; //Subscripts from the original array-3(The last element subscript is-1,-3 The element corresponding to the subscript is 7) and is intercepted from the position-1 Before subscript (excluding-1 Subscript) console.log(arr3.slice(-3,-1)); // [7, 8] Return value console.log(arr3); // [5,6,7,8,9] Original array
Spelling:
Concat (element, element,...)
- Function: splice new elements behind the specified array (if the new element is an array, remove the outermost [] and splice the inner elements)
- Return value: new array after splicing
- Affect original array: no
var arr = [5,6,7,8,9]; //If the spliced elements are multidimensional arrays, the elements in the outermost array are spliced to the back of the specified array as they are console.log(arr.concat(1,[true,[1,2]],3)); // [5, 6, 7, 8, 9, 1, true, [1,2], 3] Return value console.log(arr); // [5,6,7,8,9] Original array
Reply:
function copyArray(arr){ return arr.slice(0); } function cloneArray(arr){ return arr.concat(); }
Row:
reverse()
- Function: store the elements in the array in reverse order
- Return value: returns the array in reverse order
- Affect original array: Yes
var arr = [3,2,10,4,5]; console.log(arr.reverse()); // [5, 4, 10, 2, 3] Return value console.log(arr); // [5, 4, 10, 2, 3] Original array
sort()
- Function: sort the elements in the array from small to large according to the character code
- Return value: returns the array in reverse order
- Affect original array: Yes
var arr = [3,2,10,4,5]; //Compare as a string-Sort: 10 vs. 2 compare 1 < 2,So 10 < 2 console.log(arr.sort()); // [10, 2, 3, 4, 5] Return value console.log(arr); // [10, 2, 3, 4, 5] Original array
sort(function(a,b){return a-b;}): sort the elements in the array from small to large. sort(function(a,b){return b-a;}): sort the elements in the array from large to small according to numbers.
var arr = [3,2,10,4,5]; // From small to large by number console.log(arr.sort(function(a,b){return a-b;})); //[2, 3, 4, 5, 10] // Press numbers from large to small console.log(arr.sort(function(a,b){return b-a;})); //[10, 5, 4, 3, 2]
Turn:
join('connector ')
- Function: convert the array into a string connected with the specified connector
- Return value: returns the converted string
Affect original array: no
var arr = [1,2,3,4]; // Converts an array to a string concatenated with an empty string console.log(arr.join('')); //'1234'