Home > Web Front-end > JS Tutorial > body text

Common methods of js array operation_Basic knowledge

WBOY
Release: 2016-05-16 16:49:23
Original
1062 people have browsed it

Traversal is used more often when processing JSON arrays in jquery, but adding and removing them does not seem to be too much.

I tried json[i].remove() and json.remove(i) today but it didn’t work. Looking at the DOM object of the web page, it seems that the JSON data appears in the form of an array. I checked the relevant arrays in JS. It's really fun to try it out.

Record it.

1. Creation of array

Copy code The code is as follows:

var arrayObj = new Array(); //Create an array
var arrayObj = new Array([size]); //Create an array and specify the length. Note that it is not the upper limit, but the length
var arrayObj = new Array([element0[ , element1[, ...[, elementN]]]]); //Create an array and assign values ​​

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 at the specified length. Otherwise, please note: the length will change accordingly.

2. Access to array elements

Copy code The code is as follows:

var testGetArrValue =arrayObj[1]; //Get the element value of the array
arrayObj[1]= "This is the new value"; //Assign a new value to the array element

3. Adding array elements

Copy code The code is as follows:

arrayObj. push ([item1 [item2 [. . . [itemN ]]]]);// Add one or more new elements to the end of the array and return the new length of the array
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);// Add 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]]]]);//Insert one or more new elements into the specified position of the array. The element at the insertion position is automatically moved back and returns "".

4. Deletion of array elements

Copy code The code is as follows:

arrayObj.pop (); //Remove the last element and return the element value
arrayObj.shift(); //Remove the first element and return the element value, the elements in the array are automatically moved forward
arrayObj.splice( deletePos,deleteCount); //Delete the specified number of deleteCount elements starting from the specified position deletePos, and return the removed elements in array form

5. Interception and merging of arrays

Copy code The code is as follows:

arrayObj. slice(start, [end]); //Return 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
arrayObj.concat([item1[, item2 [, . . . [,itemN]]]]); //Concatenate multiple arrays (can also be strings, or a mixture of arrays and strings) into one array, and return the connected new array

6. Copy of array

Copy code The code is as follows:

arrayObj.slice( 0); //Returns a copy array of the array, note that it is a new array, not pointing to
arrayObj.concat(); //Returns a copy array of the array, note that it is a new array, not pointing to

7. Sorting of array elements

Copy code The code is as follows:

arrayObj.reverse (); //Reverse the elements (the first to the last, the last to the front), return the array address
arrayObj.sort(); // Sort the array elements, return the array address

8. Stringification of array elements

Copy code The code is as follows:

arrayObj .join(separator); //Return a string. This string connects each element value of the array together, separated by separator.

toLocaleString, toString, valueOf: can be regarded as special usage of join, not commonly used
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template