Home  >  Article  >  Web Front-end  >  Collection of javascript array learning materials_basic knowledge

Collection of javascript array learning materials_basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:29:55924browse

In JavaScript, you can use new Array to create an array. The following syntaxes are correct:
arrayObj = new Array() Create an array.

arrayObj = new Array([size]) Create an array and specify the length. Note that it is not the upper limit, but the length.

arrayObj = new Array([element0[, element1[, ...[, elementN]]]]) Create an array and assign values.

arrayObj = [element0, element1, ..., elementN] is the abbreviation for creating an array and assigning values. Note that the square brackets here do not mean that they can be omitted.

It should be noted that although the second method creates an array and specifies the length, in fact the array is variable-length in all cases, which means that even if the length is specified to be 5, the elements can still be stored Outside the specified length, please note: the length will change accordingly.

new Array(5) Does it mean to create an array with a length of 5 or an array with an element value of 5? Create an array of length 5.

Does the array subscript start from 0 or 1? Starts from 0, so the upper limit of the array is equal to the length of the array -1.

What is the maximum value of an array subscript? 2 to the 32nd power minus 2, that is, 4294967295, which is about 4 billion, which is enough.

Will the array subscript be rounded automatically when it is a decimal? No, it will be ignored or a runtime error will occur.

Does it support multi-dimensional arrays? Not supported! However, each element of the array can be redefined as an array to achieve the purpose of a multi-dimensional array.

How to access array elements? Use "[]", for example, the array name is arr, and to access the first element, use arr[0].

JavaScript array (JScript version 2) has a total of 3 properties and 13 methods. Among the three properties, only length is more important, but it is relatively simple. In addition, the constructor and prototype properties are common to object and are not commonly used, so the Array property will not be introduced, but the 13 methods of Array will be introduced in groups to facilitate memory.

pop and push: pop removes the last element and returns the element value; push([item1 [item2 [. . . [itemN ]]]]) adds one or more new elements to the end of the array , and returns the new length of the array. If an array is added, use commas to connect the elements of the array before adding.

shift and unshift: correspond to pop and push respectively, except that this is done at the beginning of the array. Note that removing or adding elements from the starting position will move the elements forward or backward in the array.

slice and splice: slice(start, [end]) returns a part of the array in the form of an array. Note that the element corresponding to end is not included. If end is omitted, all elements after start will be copied; splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]]) removes one or more elements from the array, if necessary, inserts a new element at the position of the removed element, and returns the removed element in array form elements. If an array is inserted, only the first element of the array is inserted.

reverse and sort: reverse() reverses the elements (first to last, last to first), and returns the array address; sort() sorts the array and returns the array address.

concat and join: concat concatenates multiple arrays (can also be strings) into one array; join(separator) returns a string, which concatenates each element value of the array together, with the middle Use separator to separate.

toLocaleString, toString, valueOf: can be regarded as special uses of join, which are not commonly used.

For more basic information, you can view: js array

Fast cloning of JavaScript arrays (slice() function) and sorting, shuffling and searching of arrays (sort() function

javascript deletes duplicate items in the array (uniq)

Deletion method of elements in the JScript built-in object Array

Javascript array usage call method summary

javascript array sorting function

Objects and arrays in javascript Application Tips

The simplest way to clone an array in JavaScript

The simplest code to create an array in javascript

Performance comparison of three JavaScript array copy methods

For more information, you can search for "javascript array" on the page to find more related information.

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