Let's take a look at the JS array and empty it.
When an array needs to be emptied, many people use the following method:
a = [];
We know that JavaScript variables are stored in the form of reference types and direct quantities. The array belongs to the object, which is the reference type, and refers to the variable pointer address, which is designed to save memory.
In the way of the empty array above, if you take a direct assignment to a new array, the previously referenced array may not be freed (with other references), such as the following code:
The code is as follows |
Copy Code |
var a = [2,3]; var B = A; a = []; Console.log (b);
|
At this time A and B is not the same array, empty A and B or refer to the previous reference address, unless you are intentional, otherwise this will be a hidden danger.
So the best way to empty an array is to set the length to 0, which is:
The code is as follows |
Copy Code |
a.length = 0; |
JS array creation, access
Creation of arrays
The code is as follows |
Copy Code |
var arrayobj = new Array (); Create an array var arrayobj = new Array ([size]); Create an array and specify the length, note not the upper limit, is the length var arrayobj = new Array ([element0[, element1[, ...) [, ELEMENTN]]]); Create an array and assign a value |
To illustrate, although the second method creates an array that specifies the length, in all cases the array is longer, that is, even if you specify a length of 5, you can still store the elements outside the specified length, note: the length changes.
2, access to elements of the array
The code is as follows |
Copy Code |
var testgetarrvalue=arrayobj[1]; Gets the element value of an array
Arrayobj[1]= "This is the new value"; Give a new value to an array element |
3, the addition of array elements
The code is as follows |
Copy Code |
Code Arrayobj. Push ([Item1 [item2 [...] [Itemn]]]); /Adds one or more new elements to the end of the array and returns the new length of the array Arrayobj.unshift ([Item1 [item2 [...] [Itemn]]]); /Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned Arrayobj.splice (insertpos,0,[item1[, item2[, ...) [, Itemn]]]); /inserts one or more new elements into the array at the specified position, and the element at the insertion point is automatically moved back to "". |
JS Array Conversion
One, the array of the string (the element of the array is connected to a string with a character)
The code is as follows |
Copy Code |
var a, B; A = new Array (0,1,2,3,4); b = A.join ("-"); |
Second, an array of strings (cut the string by one character into several strings and returned as an array)
The code is as follows |
Copy Code |
var s = "abc,abcd,aaa"; SS = S.split (",");//decompose at each comma (,). |
Array element deletion
var arr=[' A ', ' B ', ' C '];
There are two ways to delete a ' B ' in it:
1.delete method: Delete Arr[1]
This way the array length does not change, at this time arr[1] becomes undefined, but also has the advantage the index of the original array also remains unchanged, at this time to enumerate the elements of the group can be used
The code is as follows |
Copy Code |
For (index in ARR) { document.write (' arr[' +index+ ']= ' +arr[index]); } |
This traversal way skips over the elements of the undefined
It is commonly used to refactor arrays by traversing them.
The code is as follows |
Copy Code |
Array.prototype.remove=function (DX) { if (isNaN (dx) | | Dx>this.length) {return false;} for (Var i=0,n=0;i<this.length;i++) { if (THIS[I]!=THIS[DX]) { This[n++]=this[i] } } This.length-=1 } A = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']; Alert ("Elements:" +a+ "nlength:" +a.length); A.remove (0); Delete the element with subscript 0 Alert ("Elements:" +a+ "nlength:" +a.length); |
common functions for arrays
ToString (): Converts an array to a string
toLocaleString (): Converts an array to a string
join (): Converts an array to a symbolic concatenated string
Shift (): Moves an element of an array's head out
Unshift (): Inserts an element in the header of an array
Pop (): Deletes an element from the end of an array
push (): Add an element to the tail of the array
Concat (): Add elements to an array
Slice (): Returns the part of an array
Reverse (): Sort the array in reverse
sort (): Sort operations on Arrays
Splice (): Insert, delete, or replace an array element