jquery Operation JSON Array

Source: Internet
Author: User

var t = [{' A ': 1}, {' A ': 2}, {' A ': 3}, {' A ': 4}];


T.push ({' A ': 5}); Add to
T[0].A = 11; Modify
T.splice (a); Delete


for (i = 0; i < t.length; i++)
alert (T[I].A);

The splice function alone can be used to increase, delete, change

Modify the operation:

var chaomao=[1,2,3,4,5]
Chaomao.splice (2,1,8,9) alert (Chaomao)//1,2,8,9,4,5

The first parameter is the array position of the prepare operation, the second parameter is the number of array items following the position of the operation, and the third one is after the replaced content

The example is: Starting with the Chaomao array position 2 (that is, the item with value 3, array subscript starting from 0), position 2 after the item, replace Chengcheng 8,9

If the second parameter is changed to 2, that is Chaomao.splice (2,2,8,9), that is, position 2 after the two items replaced by 8, 9, the result of printing is 1,2,8,9,5,3 and 4 of the 22 items have been replaced

It should be explained here that the number of items to be replaced is not necessarily equal to the number of items replaced, 1 items can be replaced by 3 items, 5 items can also be replaced by 2 items, and based on this principle, we use this method to add and delete operations to the array

Delete operation:

var chaomao=[1,2,3,4,5]
Chaomao.splice (2,1) alert (Chaomao)//1,2,4,5

In the above example, the position in the Chaomao 2 after the 1 items are replaced with empty, because there is no content, the result can be seen, the 3 is deleted

To add an action:

var chaomao=[1,2,3,4,5]
Chaomao.splice (2,0,8,9) alert (Chaomao)//1,2,8,9,3,4,5

In the above example, the 0 items in the Chaomao position 2 are replaced by 8, 9, which is equal to adding two

In fact, the delete and add operations are just two variants of the splice modification method.

jquery Operation JSON Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.