Parse the Javascript array and add and delete JSON Elements

Source: Internet
Author: User
Tags javascript array

Three methods for deleting arrays by using explain script
1. Use shift () method
Shift: Delete the first entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
VaR chaomao = [1, 2, 3, 4, 5]
VaR chaomao. Shift () // get 1
Alert (chaomao) // [2, 3, 4, 5]

2. Use the POP () method
Pop: Delete the last entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
VaR chaomao = [1, 2, 3, 4, 5]
VaR chaomao. Pop () // get 5
Alert (chaomao) // [1, 2, 3, 4]
The preceding method can only operate the beginning and end of an array, but cannot operate the intermediate items. If you want to operate the intermediate items, use the splice method.

3. Use the splice Method
This method is very powerful. You can add, delete, or replace any array item.

Modification 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 preparation operation, the second parameter is the number of array items after the operation position, and the third parameter is the replaced content.
The example indicates that, starting from position 2 of the chaomao array (that is, the item with the value of 3, the subscript of the array starts from 0), the item after position 2 is replaced with 8 or 9.
If you change the second parameter to 2, that is, chaomao. splice (,), that is, the two items after position 2 are changed to 8, 9, and the printed results are 1, 2, 8, 9, 5, 3, and 4.
It must be noted that the number of items to be replaced is not necessarily equal to the number of items to be replaced. One item can be replaced by three items, and five items can be replaced by two items. Based on this principle, we use this method to add and delete arrays.

Delete operation:
VaR chaomao = [1, 2, 3, 4, 5]
Chaomao. splice (2, 1)
Alert (chaomao) // 1, 2, 4, 5
In the above example, replace the first item after position 2 in chaomao with an empty one. Because there is no content in the end, we can see that this item 3 is deleted.

Add operation:
VaR chaomao = [1, 2, 3, 4, 5]
Chaomao. splice (2, 0, 8, 9)
Alert (chaomao) // 1, 2, 8, 9, 3, 4, 5
In the preceding example, the value of 0 after position 2 in chaomao is changed to 8 or 9, which means that two items are added.
In fact, the delete and add operations are only derived from the splice modification method.
How to delete an object using the deletescript
Delete an object element in JS using the delete Operator
Let's look at an example.

CopyCode Code: var P = {
"Name": "chaomao ",
"Age": 45,
"Sex": "male"
};
Delete P. Name
For (var I in P ){
Console. Log (I); // The output age, sex, and name items have been deleted.
}

Add a JSON Element
Copy codeThe Code is as follows: var JSON ={}; // skip if JSON has been defined
JSON [text] = value;
JSON. text2 = value2; // at this time, text2 must comply with the variable name standard; otherwise, you must assign values using arrays.

Related Article

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.