Operation implementation code of json array under jquery_jquery
May 16, 2016 pm 06:21 PMToday 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
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
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
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
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

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Performance optimization tips for converting PHP arrays to JSON

How do annotations in the Jackson library control JSON serialization and deserialization?

PHP array key value flipping: Comparative performance analysis of different methods

PHP array multi-dimensional sorting practice: from simple to complex scenarios

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods

Application of PHP array grouping function in data sorting

The role of PHP array grouping function in finding duplicate elements
