Properties and methods of Array in JavaScript.

高洛峰
Release: 2016-11-26 13:45:08
Original
1386 people have browsed it

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


The length attribute of the array is writable, which is a very interesting Attribute, we can use this method to intercept the array


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.


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


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.


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.


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


reverse( ) method sorts the array in reverse order. It does not create and return a new array, but operates on the original array


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





Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!