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

Operation implementation code of json array under jquery_jquery

May 16, 2016 pm 06:21 PM
jquery json array

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization tips for converting PHP arrays to JSON

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

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 key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

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

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

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 The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

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 Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

Application of PHP array grouping function in data sorting

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

The role of PHP array grouping function in finding duplicate elements

See all articles