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

The addition and deletion of the JSON element


May 08, 2021 JSON



Javasscript removes arrays in 3 ways

1, using the 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 ()// Get 1
alert(chaomao)//[2,3,4,5]


2, using the 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() / / Get 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, using 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
The code is as follows:

var json = {}; Skip if Jason is already defined
json[text] = value;
json.text2 = value2; At this point text2 must meet the variable name criteria, otherwise it must be assigned an array


Arrays inside javascript, json objects, dynamic additions, modifications, delete examples
<!DOCTYPE html>
<html>
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>javascript里面的数组,json对象,动态添加,修改,删除示例</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        var a = JSON.parse("{\"title\":\"\",\"data\":[]}");
        var b = JSON.parse("{\"id\":\"2\"}");
        var c = JSON.parse("{\"id\":\"3\"}");
        var d = JSON.parse("{\"id\":\"4\"}");
        var e = JSON.parse("{\"id\":\"5\"}");
        var f = JSON.parse("{\"id\":\"6\"}");

        function myObjectPush() {
            debugger;
            /* javascript里面的数组,json对象,动态添加,修改,
删除示例 只要适合Javascript的方法都是可以用在JSON对象的数组中的!
所以还有另外的方法splice( )进行crud操作!
*/
            //增加属性
            $(a).attr("id", "1");
            //增加子对象
            a.data.push(b); //数组最后加一条记录
            a.data.push(c);
            a.data.push(d);
            a.data.unshift(d); //数组最前面加一条记录
            //修改子对象及属性
            a.title = "这是json名字";
            //删除子对象
            //json的删除有很多种,直接用过 delete json对象方式:
            delete a.data[1];
            a.data.pop(); //删除最后一项
            a.data.shift(); //删除第一项
            a.data.splice(0, 1); //删除指定子对象,参数:开始位置,删除个数
            //替换不删除
            a.data.splice(1, 0, e, f); //开始位置,删除个数,插入对象
            //替换并删除 a.data.splice(0, 1, e, f);//开始位置,删除个数,插入对象
        }
        </script>
    </head>
    
    <body onload="myObjectPush()"></body>

</html>