Manza
Manza

Reputation: 2141

Deleting element from JSON is leaving me an undefined element

I have a JSON Object, that i need to loop through and delete some elements. However after I delete an element I end up with an undefined element instead, this is causing me issues, as I am unable to use this "new JSON" (I am trying to use it within datatables)

the following is what I am currently doing

    function dTable(presId){
  var allregos = '[{"presId": "a09N0000004UbBnIAK","name": "Something","id": "001N000000Mw7knIAB"},{"presId": "a09N0000004UbBnIAK","name": "test catty","id": "001N000000O98IoIAJ"},{"presId": "a09N0000004UbBnIAK","name": "Something","id": "001N000000Mw7knIAB"}]';
  var newJson;
  var regoValue;
  for (var ke in allregos) {
    if (allregos.hasOwnProperty(ke)) {
      regoValue = allregos[ke].presId;
      if(regoValue != presId){
        delete allregos[ke];
      }else{
        //INstead of delete I could maybe create a new JSON by adding the whole node?
        //but I am unable to add the node
        //newJson = newJson.allregos[ke];
      }
    }
  } 

  console.log(allregos);
  console.log(newJson);

  j$('#myRegos').dataTable( {
      "data": newJson,
      "destroy": true,
      "columns": [
          { "title":"Name", "mData": "name", "class": "center" },
      ]
  } );
}

my console log shows me something like:

[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, Object { presId="a09N0000004W3YLIA0", name="name 1", id="001N000000Mw7knIAB"}, Object { presId="a09N0000004W3YLIA0", name="Call Centre", id="001N000000MvDaMIAV"}, Object { presId="a09N0000004W3YLIA0", name="Who Is", id="001N000000MvIiaIAF"}]

is there a way to get rid of the undefiend elements?

The other work around that I was thinking was instead of deleting the elements add the appropiate ones into a new JSON however I am not able to do this. I was trying something like:

newJson = newJson.allregos[ke];

Upvotes: 2

Views: 1542

Answers (2)

ismnoiet
ismnoiet

Reputation: 4159

Here we have allRegos as an array of objects so a better way to use it is to take advantage of the standard for without using for (var ke in allregos) and then if (allregos.hasOwnProperty(ke)) and also i'm converting the json string to a real json data by calling JSON.parse(allregos) below is the enhanced dTable function note also that i added another object with presId='a09N0000004UbBnIAz' just to see if we are getting the right answer when we call the dTable('a09N0000004UbBnIAK');

function dTable(presId){
   allregos = '[{"presId": "a09N0000004UbBnIAK","name": "Something","id": "001N000000Mw7knIAB"}, {"presId": "a09N0000004UbBnIAK","name": "test catty","id": "001N000000O98IoIAJ"}, {"presId": "a09N0000004UbBnIAK","name": "Something","id": "001N000000Mw7knIAB"},{"presId": "a09N0000004UbBnIAz","name": "Something","id": "001N000000Mw7knIAB"}]'; 
   oldJson = allregos; 
   var regoValue;

allregos = JSON.parse(allregos);
oldJson = JSON.parse(oldJson);
newJson = [];



  for( i=0;i<allregos.length;i++){
    regoValue = allregos[i].presId;
    if(regoValue != presId){
        delete allregos[i];
    }else{
        newJson.push(allregos[i]);
    }
  }

  console.log('allregos before:');
  console.log(oldJson);
  console.log('allregos after:')
  console.log(allregos);
  console.log('newJson after:')
  console.log(newJson);

}

Upvotes: 1

Dani&#235;l Camps
Dani&#235;l Camps

Reputation: 1809

You can get rid of the elements by completely overwriting the object.

Say you have

var myObject = {
"keyFoo" : "valueFoo",
"keyBar" : "valueBar",
"keyBaz" : undefined
};

in order to find out which values are not undefined, you can gothrough your object, and only set values which return true. In this scenario, you can isolate "keyBaz", because (x.keyBaz) returns false when coerced into a boolean context, whereas (x.keyFoo) and (x.KeyBar) both return true when coerced into a boolean context. You can do also do this within an array.

You can then overwrite the object by doing:

myObject  = {
"keyFoo" : "valueFoo",
"keyBar" : "valueBar"
};

Upvotes: 0

Related Questions