This article mainly introduces the knowledge about arrays in JavaScript, and is the basic knowledge of JS beginners. For more information, see
Create an array
The array declaration in js can be in the following ways:
Var arr = []; // shorthand mode var arr = new Array (); // a new array object var arr = new Array (arrayLength ); // new an array object with a fixed length
Note:
- Although the third method declares the length of the array, the length of the array is actually variable. That is to say, even if the length is set to 5, the elements can still be stored outside the specified length, and the length of the array will change accordingly.
- In addition, it should be clear that:
- Js is a weak type language, that is, the element types in the array do not need to be the same.
For example, the element types in the array are inconsistent:
var arr = [1, 2, 3, 4, 'wangzhengyi', 'bululu'];for (var i = 0; i < arr.length; i ++) { console.log(arr[i]);}
Array Element access
The index value of the JavaScript array also starts from 0. We can directly access the array elements by using the array name + the following method.
The sample code is as follows:
var arr = [1, 2, 3];console.log(arr[0]);console.log(arr[1]);
In addition, the continuous for Loop mode is recommended for array traversal. for more information, see Loop through array in JavaScript.
Sample Code for traversing an array is as follows:
var arr = [1, 2, 3, 4, 'wangzhengyi', 'bululu'];for (var i = 0, len = arr.length; i < len; i ++) { console.log(arr[i]);}
Note:
In the above Code, a small optimization is to get the size of the array in advance, so that you do not need to query the array size every time you traverse. For ultra-large arrays, the efficiency can be improved.
Add array elements
There are three ways to add new elements to an array: push, unshift, and splice. The following describes the three methods.
Push
Push method. add elements at the end of the array. The sample code is as follows:
var arr = [];arr.push(1);arr.push(2);arr.push(3);for (var i = 0, len = arr.length; i < len; i ++) { console.log(arr[i]);}
The execution result is:
123
Unshift
The unshift method adds an element to the array header. The sample code is as follows:
var arr = [];arr.unshift(1);arr.unshift(2);arr.unshift(3);for (var i = 0, len = arr.length; i < len; i ++) { console.log(arr[i]);}
The execution result is as follows:
321
Splice
The splice method inserts new elements at the specified position of the array, and the previous elements are automatically moved after order. Note that the prototype of the splice function is:
array.splice(index, howMany, element...)
How indicates the number of elements to be deleted. If you only add an element, howMany must be set to 0.
The sample code is as follows:
var arr = [1, 2, 3, 4];arr.splice(1, 0, 7, 8, 9);for (var i = 0, len = arr.length; i < len; i ++) { console.log(arr[i]);}
The execution result is as follows:
1789234
Delete array elements
Like adding an array element, there are three methods to delete the elements in the array: pop, shift, and splice. Next, we will explain the usage of these three functions.
Pop
The pop method removes the last element from the array. The combination of push and pop can implement the array function similar to stack (first in and then out. The sample code is as follows:
var arr = [];arr.push(1);arr.push(2);arr.push(3);while (arr.length != 0) { var ele = arr.pop(); console.log(ele);}
Shift
The shift method removes the first element, and the elements in the array are automatically moved forward. (This method certainly corresponds to the efficiency problem, and the time complexity is O (n )).
var arr = [];arr.push(1);arr.push(2);arr.push(3);function traverseArray(arr) { for (var i = 0, len = arr.length; i < len; i ++) { console.log(arr[i]); }}while (arr.length != 0) { var ele = arr.shift(); traverseArray(arr);}
You can consider the running result on your own.
Splice
When adding array elements, we will talk about splice. This function prototype has a howMany parameter, which indicates the number of elements deleted from index.
The sample code is as follows:
var arr = [1, 2, 3, 4, 5, 6, 7];function traverseArray(arr) { for (var i = 0, len = arr.length; i < len; i ++) { console.log(arr[i]); }}arr.splice(1, 3);traverseArray(arr);
The execution result is:
157
Copying and intercepting Arrays
For example, the Code is as follows:
var arr1 = [1, 2, 3, 4];var arr2 = arr1;
At this time, arr2 only saves the address of the arr1 array in the heap memory, and does not apply for a new array in the heap memory. Therefore, the modification to arr2 affects arr1 at the same time. Therefore, what should we do if we need to copy a number of copies? This leads to the slice and concat functions to be learned.
Slice
The slice here is the same as the slice in the python syntax, and is the slice that returns an array. The slice function is prototype:
array.slice(begin, end)
Returns all elements from begin to end. Note that the elements include begin, but not end.
The default value is begin. The default value starts from 0. The default end is the end of the array.
Therefore, we can use the following code to copy an array:
var arr1 = [1, 2, 3, 4];var arr2 = arr1.slice();arr2[2] = 10000function traverseArray(arr) { for (var i = 0, len = arr.length; i < len; i ++) { console.log(arr[i]); }}traverseArray(arr1);traverseArray(arr2);
The execution result is as follows:
123412100004
Concat
The concat method creates a new array, and then calls its object (the object that this points) elements and elements in all parameters of the array type and parameters of the non-array type are placed in the new array in order, and the array is returned.
The sample code is as follows:
Var alpha = ["a", "B", "c"]; var number = [1, 2, 3] // The new array is ["", "B", "c", 1, 2, 3] var complex = alpha. concat (number );