Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Resolve javascript arrays (and add and remove jason elements)


May 08, 2021 JSON


Table of contents


Javasscript removes arrays in 3 ways

1, with shift() method

shift: Deletes the first item of the original array and returns the value of the deleted element;

var chaomao=[1,2,3,4,5]
var chaomao.shift()//得到1
alert(chaomao)//[2,3,4,5]

2, with pop() method

pop: Delete the last item of the original array and return the value of the deleted element;

var chaomao=[1,2,3,4,5]
var chaomao.pop()//得到5
alert(chaomao)//[1,2,3,4]

The previous method can only manipulate the beginning and end of the array, the middle item cannot be operated, and if you want to manipulate the middle item, use the space method

3, with the splice method

This method is very powerful, you can add, delete, and replace any item in the array

Modifications:

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

The first argument is the array position to prepare the operation, the second argument is the number of array items after the operation position, and the third is, after the replaced content
The example is that the item after position 2 is replaced by 8,9, starting with the array position 2 (that is, the item with the value 3, the array substation starts at 0).
If you change the second parameter 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 printed result is 1, 2, 8, 9, 5, 3 and 4
It should be noted here that the number of items replaced does not have to be equal to the number of items replaced, 1 item can be replaced with 3 items, 5 items can also be replaced with 2 items, based on this principle, we use this method to add and delete the array

Delete:

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

In the example above, replace 1 item after position 2 in chaomao with an empty one, because there is nothing behind it, and as a result, 3 items are deleted


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 example above, replacing 0 items after position 2 in chaomao with 8,9 is equivalent to adding two items
In fact, deletion and addition are just two derivatives of the splice modification method

The method by which javasscript deletes objects

js removes object elements with the delete operator
Let's look at an example

The code is as follows:
var p ={
"name": “chaomao”,
"age":45,
"sex":"male"
};
delete p.name
for(var i in p){
console.log(i);//输出age,sex,name项已经被删除了
}
The method of adding the jason element
var json = {}; // 如果json已经定义就跳过
json[text] = value;
json.text2 = value2;  // 此时text2必须符合变量名标准,否则得使用数组方式赋值
The code is as follows:
var json = {}; // 如果json已经定义就跳过
json[text] = value;
json.text2 = value2;  // 此时text2必须符合变量名标准,否则得使用数组方式赋值

The difference between a js array and a jason

One, array

1. Define a one-dimensional array:

var s1=new Array();
s1, 2, 3, 4, or s1, 0, s1, 1, 2, s1, 3, s1, 3, s1, 4, 4;
alert(s1[0]);
The result is 1;


2. Define a two-dimensional group:

var s1=new Array();
var s1=[[3,1],[2,3,4],3,[4,5,6,7,8]];
alert(s1[1][0]);
The result is 2;

Second, define the json object

1, json object
var status_process = {
" name5" : '闲置期',
"name1" : '播种期',
"name2" : '苗期',
"name3" : '生长期',
"name4" : '采收期'
 }    
alert(status_process);
The result is: Object:Object;

2, json string
The so-called jason string means that the value of the string variable is the same as the json's format, but not the johnson object, for example:
var s1="{";
var s2 = " 'name5' : '闲置期',  'name1' : '播种期','name2' : '苗期','name3' : '生长期','name4' : '采收期'";
var s3="}";
var status_process=s1+s2 +s3;
Although status_process the value of the object conforms to the format of the jason object, it is not an object, it is just a string (it is pieced together);
Convert the string to a jason object using the function eval, eval (" status_process """
Conclusion: From the background to the fore desk is the jason string, not the real jason object, so you need to use the eval function conversion.
3, the use of json objects
var status_process = {
name5 : '闲置期',
name1 : '播种期',
name2 : '苗期',
name3 : '生长期',
name4 : '采收期'
};
alert(status_process["name5"]);
alert(status_process.name5);
Both are: idle periods

4, json two-dimensional object
var status_process = {
 name5 : {name3:'空闲闲置期'},
 name1 : '播种期',
 name2 : '苗期',
 name3 : '生长期',
 name4 : '采收期'
};
alert(status_process["name5"]["name3"]);
alert(status_process.name5.name3);
The result is: 'Idle idle period'