0

I have a JSON data and I want to delete all the items in the array and I have already tried delete, Products.length = 0 but nothing works. I am able to add a new item, modify and delete a particular one but unable to remove all.

The JSON Data

{
   "Products":[
      {
         "productID":"75",
         "productName":"Mango Juice",
         "productQuantity":3,
         "seller_1":"30.00",
         "seller_2":"40.00",
         "seller_3":"34.50"
      },
      {
         "productID":"25",
         "productName":"Apple Juice",
         "productQuantity":1,
         "seller_1":"70.00",
         "seller_2":"74.00",
         "seller_3":"84.50"
      },
      {
         "productID":"10",
         "productName":"Orange Juice",
         "productQuantity":1,
         "seller_1":"10.00",
         "seller_2":"20.00",
         "seller_3":"12.50"
      }
   ]
}

After deleting all the items, I should get this as final

{"Products": []}

Here is the Jquery process

$(document).ready(function() {
    $.getJSON( "data.json", function(data) {
           data.Products = [];
            $.ajax({
            type: "POST",
            url: "json.php",
            dataType: 'json',
            data: { json: data }
        });
    });
});

The Php process

if(isset($_POST['json'])) {
    $fp = fopen('data.json', 'w+');
    fwrite($fp, json_encode($_POST['json']));
    fclose($fp);
}

console.log(data); shows that the code has been well executed on javascript side but it does not update the JSON via PHP

Mauritius
  • 105
  • 2
  • 7
  • If you have JSON you have a string - so make up your mind: do you have a JSON *STRING* or do you have an Object with an Array inside under hash key "Products"? Obviously changing a (JSON) string requires a very different approach from working with Object/Array structures. If you have an Array and not a string - see here: http://stackoverflow.com/questions/500606/javascript-array-delete-elements – Mörre Dec 07 '13 at 20:24
  • Actually - see the bottom-most answer for this question, with 1186 upvotes, 5 times more than the accepted answer: http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript – Mörre Dec 07 '13 at 20:35

2 Answers2

2

If you have this:

var data = {
   "Products":[
      {
         "productID":"75",
         "productName":"Mango Juice",
         "productQuantity":3,
         "seller_1":"30.00",
         "seller_2":"40.00",
         "seller_3":"34.50"
      },
      {
         "productID":"25",
         "productName":"Apple Juice",
         "productQuantity":1,
         "seller_1":"70.00",
         "seller_2":"74.00",
         "seller_3":"84.50"
      },
      {
         "productID":"10",
         "productName":"Orange Juice",
         "productQuantity":1,
         "seller_1":"10.00",
         "seller_2":"20.00",
         "seller_3":"12.50"
      }
   ]
}

Then, you can just do:

data.Products = [];

That will just set data.Products to be a newly created empty array.

If you have any reason to want to keep the same array in place (e.g. references you want to now be pointing to the empty array), you can also do:

data.Products.length = 0;

Note: there are many other ways to do this also including splicing all the elements out, popping all the elements out, etc... Modifying the original array by removing its items rather than assigning a new empty array will modify the array that any outside references might also be pointing to (which you may or may not want depending upon the circumstances).


FYI, JSON is a text format. What you have here is a javascript object. The two are not the same.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • That's what I would do but I just found this and there is overwhelming support for setting `array.length=0` (the **1000+ upvotes answer is at the bottom!!!**): http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript Hmmm.... – Mörre Dec 07 '13 at 20:32
  • @Mörre - There is no discussion of anyone needing to keep references intact to the original array which is the main advantage of setting `.length`. – jfriend00 Dec 07 '13 at 20:35
  • It does not look to me like an option that one should only choose in that special situation - besides, if you always do it that way you don't need to check if you have any other references, which you will have to keep in mind if you do it in any other way. So knowing we are human from now on **I** will go with setting length to 0, because I need my limited brain power for other things than checking if all my arrays may have any additional references pointing to them :) One should use the option that ALWAYS works other one where you have to think. – Mörre Dec 07 '13 at 20:37
  • @Mörre - I added some info to my answer about other ways to do it, but absent any other requirements (performance, references, etc...) and the OP's question about just looking for a way to do it, I don't think we need to go into all the possible ways to do it and all the possible pros/cons. I'm trying to not make this more complicated than was asked for. – jfriend00 Dec 07 '13 at 20:39
  • I have and had no intention to have a discussion with you - but for the record and the benefit of OP I wanted to have the link to that other Q here. I think that's fair to all. And by the way that upvote was/is mine. – Mörre Dec 07 '13 at 20:41
  • @Mörre - sounds good. FYI, there are legitimate reasons to want a new, empty array that is separate from the previous array or legitimate reasons to want to keep the same array going forward. Neither is more correct than the other - it depends upon whether you have any other code that has references and whether you want the other references to see a change in the array or not. Neither of these issues were mentioned by the OP so the default assumption would be that there are not outside references present. – jfriend00 Dec 07 '13 at 20:44
  • @Mauritius - look at the networking tab in the Chrome debugger and see what is actually being sent to your server. That will tell you whether your issue is on the client or on the server. – jfriend00 Dec 07 '13 at 21:02
0

You can try using the splice function

var arr =[1,2,3,4];
arr.splice(0,arr.length);
Mörre
  • 5,699
  • 6
  • 38
  • 63
Rohit
  • 5,631
  • 4
  • 31
  • 59