JAVASCRIPT: how can I remove a json object in json object?

Question

how can I remove a json object in json object?

[
    [
        {"name":"formtype","value":"ordering"},          
        {"name":"extera_Ordering_json[id]","value":"1"}
   ],
   [
        {"name":"extera_Ordering_json[count]","value":"20"},               
        {"name":"coupon","value":""},
        {"name":"formtype","value":"ordering"}, 
        {"name":"extera_Ordering_json[id]","value":"2"}
   ],
   [
       {"name":"extera_Ordering_json[count]","value":"7"},
       {"name":"coupon","value":"1"},
       {"name":"formtype","value":"ordering"},
       {"name":"extera_Ordering_json[id]","value":"3"}
   ],
   [   {"name":"extera_Ordering_json[count]","value":"1"},
       {"name":"coupon","value":"1"},
       {"name":"formtype","value":"ordering"},
       {"name":"extera_Ordering_json[id]","value":"4"}
   ]
]

I save this json in ordering

Now I want remove

   [   {"name":"extera_Ordering_json[count]","value":"1"},
       {"name":"coupon","value":"1"},
       {"name":"formtype","value":"ordering"},
       {"name":"extera_Ordering_json[id]","value":"4"}
   ]

My key is extera_Ordering_json[id] (it is not unique)

I do this:

ordering = JSON.parse(ordering);
var temp;
$.each(ordering, function(idx, obj)
{
    if(obj.Ordering_json[id] != 4)// not works
    {
        temp[] = obj;
    }
    temp[] = obj
}
ordering = JSON.stringify(temp);

Show source
| javascript   | jquery   | json   2017-01-04 11:01 6 Answers

Answers ( 6 )

  1. 2017-01-04 11:01

    You are using id instead of idx. try this

    if(obj.Ordering_json[idx] != 4)
    
  2. 2017-01-04 11:01

    you can use splice method

    ordering = [ /*Array without json parsing*/ ];
    var indexToDelete = 3;
    ordering.splice( indexToDelete, 1); 
    // 1 indicates only one element from array 
    console.log(ordering);
    

    Second solution is you can delete that from array

    ordering = [ /*Array without json parsing*/ ];
    var indexToDelete = 3;
    delete ordering[ indexToDelete ];
    console.log(ordering);
    
  3. 2017-01-04 11:01

    "extera_Ordering_json[id]" is simply a string so you need to iterate all the objects in each array and find the one that has that string as name and check the value of the same object

    Here's a map and filter approach

    var data = [
        [
            {"name":"formtype","value":"ordering"},          
            {"name":"extera_Ordering_json[id]","value":"1"}
       ],
       [
            {"name":"extera_Ordering_json[count]","value":"20"},               
            {"name":"coupon","value":""},
            {"name":"formtype","value":"ordering"}, 
            {"name":"extera_Ordering_json[id]","value":"2"}
       ],
       [
           {"name":"extera_Ordering_json[count]","value":"7"},
           {"name":"coupon","value":"1"},
           {"name":"formtype","value":"ordering"},
           {"name":"extera_Ordering_json[id]","value":"3"}
       ],
       [   {"name":"extera_Ordering_json[count]","value":"1"},
           {"name":"coupon","value":"1"},
           {"name":"formtype","value":"ordering"},
           {"name":"extera_Ordering_json[id]","value":"4"}
       ]
    ]
    var searchVal = 1
    data=data.map(function(arr){
       return arr.filter(function(o){
          return o.name !== "extera_Ordering_json[id]" || +o.value !== searchVal
       })
    })
    
    console.log(data)

  4. 2017-01-04 11:01

    You could use the splice method on your JSON array to remove an element at a specific index:

    var ordering = JSON.stringify([
        [
            {"name":"formtype","value":"ordering"},          
            {"name":"extera_Ordering_json[id]","value":"1"}
       ],
       [
            {"name":"extera_Ordering_json[count]","value":"20"},               
            {"name":"coupon","value":""},
            {"name":"formtype","value":"ordering"}, 
            {"name":"extera_Ordering_json[id]","value":"2"}
       ],
       [
           {"name":"extera_Ordering_json[count]","value":"7"},
           {"name":"coupon","value":"1"},
           {"name":"formtype","value":"ordering"},
           {"name":"extera_Ordering_json[id]","value":"3"}
       ],
       [   {"name":"extera_Ordering_json[count]","value":"1"},
           {"name":"coupon","value":"1"},
           {"name":"formtype","value":"ordering"},
           {"name":"extera_Ordering_json[id]","value":"4"}
       ]
    ])
    
    temp = JSON.parse(ordering);
    temp.splice(3, 1)
    ordering = JSON.stringify(temp);
    console.log(ordering)

  5. 2017-01-04 11:01

    in your case extera_Ordering_json[id] is not key, it is value of name

        var ordering = [
            [
                {"name": "formtype", "value": "ordering"},
                {"name": "extera_Ordering_json[id]", "value": "1"}
            ],
            [
                {"name": "extera_Ordering_json[count]", "value": "20"},
                {"name": "coupon", "value": ""},
                {"name": "formtype", "value": "ordering"},
                {"name": "extera_Ordering_json[id]", "value": "2"}
            ],
            [
                {"name": "extera_Ordering_json[count]", "value": "7"},
                {"name": "coupon", "value": "1"},
                {"name": "formtype", "value": "ordering"},
                {"name": "extera_Ordering_json[id]", "value": "3"}
            ],
            [{"name": "extera_Ordering_json[count]", "value": "1"},
                {"name": "coupon", "value": "1"},
                {"name": "formtype", "value": "ordering"},
                {"name": "extera_Ordering_json[id]", "value": "4"}
            ]
        ];
        var temp = [];
        $.each(ordering, function (key1, obj1) {
            $.each(obj1, function (key2, obj2) {
                if (obj2.name == 'extera_Ordering_json[id]' && obj2.value == 4) {
                    delete ordering[key1];
                }
            });
            if(ordering[key1] != null){
                temp.push(ordering[key1]);
            }
        });
        console.log(JSON.stringify(temp));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  6. 2017-01-04 12:01

    This should do what you want:

    var ordering = [
        [
            {"name":"formtype","value":"ordering"},          
            {"name":"extera_Ordering_json[id]","value":"1"}
        ],
        [
            {"name":"extera_Ordering_json[count]","value":"20"},               
            {"name":"coupon","value":""},
            {"name":"formtype","value":"ordering"}, 
            {"name":"extera_Ordering_json[id]","value":"2"}
        ],
        [
           {"name":"extera_Ordering_json[count]","value":"7"},
           {"name":"coupon","value":"1"},
           {"name":"formtype","value":"ordering"},
           {"name":"extera_Ordering_json[id]","value":"3"}
        ],
        [   
           {"name":"extera_Ordering_json[count]","value":"1"},
           {"name":"coupon","value":"1"},
           {"name":"formtype","value":"ordering"},
           {"name":"extera_Ordering_json[id]","value":"4"}
        ]
    ];
        var sVal = 4;
        var indexToRemove = false;
        ordering.forEach(function(element, index) {
           element.forEach(function(subelement) {
               if (subelement.name == "extera_Ordering_json[id]" 
                  && parseInt(subelement.value) == sVal){
                  indexToRemove = index;
               }
           });
        });
    
        if (false !== indexToRemove) {
            ordering.splice(indexToRemove, 1);
        }
    
◀ Go back