Home >Web Front-end >JS Tutorial >Operation implementation code of json array under jquery_jquery

Operation implementation code of json array under jquery_jquery

WBOY
WBOYOriginal
2016-05-16 18:21:37958browse

Today I tried json[i].remove() and json.remove(i) but it didn't work. It seems that JSON data appears in the form of an array in the DOM object of the web page. I checked the related operations of arrays in JS and tried it. It's really cool.
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, the actual length In all cases above, the array is variable-length, which means that even if the length is specified as 5, elements can still be stored outside the specified length. Note: the length will change accordingly.
2. Access to the elements of the array
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. Array element Add
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 ]]]] );// Start by adding one or more new elements to 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 "" is returned.

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 / /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 Copy code


The code is as follows:

arrayObj.slice(0); //return The copy array of the array, note that it is a new array, not pointing to
arrayObj.concat(); //Return the copy array of the array, note that it is a new array, not pointing to 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 Copy code


The code is as follows:

arrayObj.join(separator ); //Returns a string. This string connects each element value of the array together, separated by separator.
toLocaleString, toString, valueOf: can be regarded as special uses of join, not commonly used
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