Home  >  Article  >  Web Front-end  >  Properties and methods of Array in JavaScript.

Properties and methods of Array in JavaScript.

高洛峰
高洛峰Original
2016-11-26 13:45:081399browse

There are four ways to define arrays

Use the constructor:
var a = new Array();
var b = new Array(8);
var c = new Array("first", "second", "third ");
or array literal:
var d = ["first", "second", "third"];

Attribute

Array has only one attribute, which is length, and length represents the memory space occupied by the array. number, not just the number of elements in the array. In the array just defined, the value of b.length is 8

<script><br>var a = new Array("first", "second", "third ")<br>a[48] = "12"<br>document.write(a.length)<br>//The displayed result is 49<br></script>
The length attribute of the array is writable, which is a very interesting Attribute, we can use this method to intercept the array

<script><br>var a = new Array("first", "second", "third")<br>delete a[1]<br>document.write(a.length )<br>//The displayed result is 3, indicating that the length of the array cannot be changed even if deleted<br>var a = new Array("first", "second", "third")<br>a.length = 1<br>document.write(a .length)<br>//The displayed result is 1, indicating that there is only one element left<br></script>
Methods

This does not include some methods that are incompatible with IE and FF:
toString(): convert the array Convert to a string
toLocaleString(): Convert the array to a string
join(): Convert the array to a symbolically connected string
shift(): Remove an element from the head of the array
unshift() : Insert an element at the head of the array
pop(): Delete an element from the end of the array
push(): Add an element to the end of the array
concat(): Add an element to the array
slice(): Return the array's Part
reverse(): Sort the array in reverse order
sort(): Sort the array
splice(): Insert, delete or replace an array element

toString() method, toLocaleString() method has a similar function, FF The function below is exactly the same. In IE, if the element is a string, a space will be added after ",". If the element is a number, it will be extended to two decimal places. Both will change the length attribute of the string, so Considering compatibility, try not to use the toLocaleString() method.

<script><br>var a = new Array(1, 2, 3, [4, 5, [6, 7]])<br>var b = a.toString() //b is "1 in the form of a string , 2, 3, 4, 5, 6, 7"<br>var c = new Array(1, 2, 3, [4, 5, [6, 7]])<br>var d = c.toLocaleString() //d In the form of string "1, 2, 3, 4, 5, 6, 7"<br>//toString() method and toLocaleString() method can disassemble multi-dimensional arrays<br></script>
join() method All elements in the array are converted into strings and then concatenated, which is the opposite operation of String's split() method. join() uses "," as the delimiter by default. Of course, you can also specify the delimiter in the method

<script><br>var a = new Array("first", "second", "third")<br>var s = a.join("...")<br>document.write(s)<br>//The displayed result is "first...second...third"<br></script>
pop() method can be obtained from The push() method deletes several elements from the end of the array and adds an element to the end of the array. These two methods are exactly two opposite operations. Both operate on the original array, but please note that the push() method returns the length of the new array, while the pop() method returns the deleted element.

<script><br>var a = new Array(1, 2, 3)<br>var b = a.push(4,5,[6,7]) //a is [1, 2, 3, 4, 5, [6, 7]] b is 6 Note that the tbpush() method will not help you open an array<br>var c = new Array(1, 2, 3, 4, "first")<br>var d = c.pop( ) //c is [1, 2, 3, 4] d is "first" in the form of a string<br></script>
The shift() method can delete an element from the head of the array, and the unshift() method can remove several elements Adding to the head of an array, these two methods are just opposite operations. Both operate on the original array, but please note that the unshift() method returns the length of the new array, while the shift() method returns the deleted element.

<script><br>var a = new Array(1, 2, 3)<br>var b = a.unshift(4,5,[6,7]) //a is [4, 5, [6, 7 ], 1, 2, 3] b is 6 Note that the unshift() method will not help you open an array, and the order in which the values ​​are inserted is <br>var c = new Array("first", 1, 2, 3, 4 )<br>var d = c.shift() //c is [1, 2, 3, 4] d is "first" in the form of a string<br></script>
concat() method can return a new array on the original array An array of elements is added. The elements are separated by ",". If there is an array in the element, it will be expanded and added continuously, but expansion and addition in the form of multi-dimensional arrays are not supported

<script><br>var a = new Array("first", "second", "third")<br>s = a.concat("fourth",["fifth", "sixth"],["seventh", ["eighth", "ninth"]])<br>document.write(s[7])<br>//The displayed result is "eighth, ninth", indicating that "eighth, ninth" is added in the form of an array, This is the value of s as ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", ["eighth", "ninth"]]<br></script&gt ;<br>slice() method returns a slice of the array, or a subarray. The parameters of slice() represent the beginning and end positions of the word array. If there is only one parameter, it means taking it from there to the end. If the parameter is negative, it means a certain position of the reciprocal. slice(start,end) //Indicates that the array starts from the subscript start (including this) to end (excluding this)</p> <p><script><br>var a = new Array(1, 2, 3, 4 , 5)<br>var b = a.slice(3) //b is [4, 5]<br>var c = a.slice(-3) //c is [3, 4, 5]<br>var d = a. slice(1,-1) //d is [2, 3, 4]<br>var e = a.slice(-3,-1) //e is [3, 4]<br></script>
reverse( ) method sorts the array in reverse order. It does not create and return a new array, but operates on the original array

<script><br>var a = new Array("first", "second", " third")<br>a.reverse()<br>document.write(a)<br>//The displayed result is "third, second, first". At this time, the order of the array has been reversed<br></script>
sort() method The function is to sort the array. This is a very peculiar method. I don't know whether the person who created it was out of laziness or cleverness. This is a method that impressed me deeply. The parameter of the
sort() method is a function with two parameters and a return value. If the returned value is greater than zero, it means that the previous parameter is larger than the next parameter. If it is equal to zero, it is equal. If it is less than zero, it means that the previous parameter is larger than the last parameter. The latter one is smaller, and the relatively smaller parameter will appear at the front of the sort. The
sort() method operates directly on the array and also returns a value, but the two seem to be equivalent. The sort() method defaults to sorting in alphabetical order

<script><br>var a = new Array(33, 4, 111, 543)<br>a.sort(way)<br>function way(x, y){<br> if (x % 2 ==0) <br>                                                                                 Use with use using                 through out through ‐ off off off off off‐ back out ‐ if (x % 2 == 0)      The function of the <br>splice() method is to insert, delete or replace an array element. It will not only modify the original array, but also return the processed content. Therefore, this method is powerful but not easy to use. method, the splice() method uses the first two parameters for positioning, and the remaining parameters represent the insertion part. <br><br><script><br>var a = new Array(1, 2, 3, 4, 5)<br>var b = a.splice(2) //a is [1, 2] b is [3, 4, 5 ]<br>var c = new Array(1, 2, 3, 4, 5)</p>var d = c.splice(2,2) //c is [1, 2, 5] d is [3, 4]<p>var e = new Array(1, 2, 3, 4, 5)<br>var f = f.splice(-4,2) //e is [1, 4, 5] f is [2, 3]<br>var g = new Array(1, 2, 3, 4, 5)<br>var h = g.splice(-2,-2) //The second parameter represents the length, so negative numbers are invalid here<br> <br>var i = new Array(1 , 2, 3, 4, 5)<br>var j = i.splice(2,2,"first","second","third") //i is [1, 2, "first", "second", "third", 5] j is [3, 4] and the latter part will automatically move forward and backward to maintain the continuity of the array<br>var k = new Array(1, 2, 3, 4, 5)<br>var l = k.splice (2,2,["first","second"],"third") //k is [1, 2, ["first", "second"], "third", 5] l is [3, 4 ] The splice() method does not expand the array, it only writes directly<br></script>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn