Parsing JavaScript arrays and adding and removing JSON elements

Source: Internet
Author: User
Tags arrays json

Javasscript 3 ways to delete an array
1, using the Shift () method
Shift: Deletes the first item of the original array and returns the value of the deleted element, or returns undefined if the array is empty
var chaomao=[1,2,3,4,5]
var chaomao.shift ()//Get 1
Alert (CHAOMAO)//[2,3,4,5]

2, using Pop () method
pop: Deletes the last item of the original array and returns the value of the deleted element, or returns undefined if the array is empty
var chaomao=[1,2,3,4,5]
var chaomao.pop ()//Get 5
Alert (CHAOMAO)//[1,2,3,4]
The previous method can only manipulate the beginning and end of the array, cannot manipulate the middle item, and if you want to manipulate the middle item, use the Splice method

3, using Splice method
This method is very powerful, you can increase the number of arbitrary items, delete, replace the operation

To modify an 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 to prepare the operation, the second parameter is the number of array items after the operation position, and the third is after the replaced content
The example is the expression: starting with the array position 2 (that is, the value 3, the array subscript starting from 0), the position 2 Chaomao, replace into 8,9
If the second argument is changed to 2, that is, Chaomao.splice (2,2,8,9), that is, the two items after position 2 are replaced by 8, 9, and the result of the printout is 1,2,8,9,5,3 and 4, the 22 items have been replaced.
What needs to be explained here is that the number of items replaced is not necessarily equal to the number of items replaced, 1 items can be replaced by 3, 5 can also be replaced by 2, based on this principle, we use this method to add and remove the array

Delete action:
var chaomao=[1,2,3,4,5]
Chaomao.splice (2,1)
Alert (Chaomao)//1,2,4,5
In the example above, replace the 1 items in the Chaomao position 2 to empty, because there is no content, the result can be seen, the 3 of this deleted

Add 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 example above, replacing the 0 items in the Chaomao position 2 with 8, 9 is equivalent to adding two
In fact, deletion and addition operations are just two kinds of derivation of splice modification method
Javasscript ways to delete objects
Delete object element in JS with the delete operator
Let's take a look at an example

Copy Code code as follows:


var p ={


"name": "Chaomao",


"Age": 45,


"Sex": "Male"


};


Delete P.name


for (var i in P) {


Console.log (i)//Output Age,sex,name item has been deleted


}


ways to add JSON elements

Copy Code code as follows:


var json = {};//If JSON is already defined, skip
Json[text] = value;
Json.text2 = value2//At this point Text2 must conform to the variable name standard, otherwise you have to use an array method to assign the value

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.