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.
Copy codeThe Code is as follows:
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.