0

I am getting so many objects in a obj.Like this

Object {MAILING ADDRESS: "P O BOX 59", APN: "066-102-11-1"}
Object {MAILING ADDRESS: "", APN: ""}
Object {MAILING ADDRESS: "P O BOX 3", APN: "066-105-11-1"}
Object {MAILING ADDRESS: "", APN: ""} 

When I apply a delete function on Null values of It I get this

Object {MAILING ADDRESS: "P O BOX 59", APN: "066-102-11-1"}
Object {}
Object {MAILING ADDRESS: "P O BOX 3", APN: "066-105-11-1"}
Object {}

I want to delete completely object{} and want to get only 2 Objects which have values. My code for deleting this values is

(function filter(obj_field) {
  $.each(obj_field, function(key, value) {
    if (value === "" || value === null) {
      delete obj_field.key;
    } else if (Object.prototype.toString.call(value) === '[object Object]') {
      filter(value);
    } else if ($.isArray(value)) {
      $.each(value, function(k, v) {
        filter(v);
      });
    }
  });
})(obj_field);
SAMUEL
  • 8,098
  • 3
  • 42
  • 42
  • Possible duplicate of http://stackoverflow.com/questions/742623/deleting-objects-in-javascript – Sagar May 09 '17 at 12:22
  • I'm kinda sure you asked something like this earlier today. But again I'm gonna ask, can you please make a working example of your problem – Carsten Løvbo Andersen May 09 '17 at 12:23
  • 2
    `delete obj_field.key` - this deletes the key of the object. Use `delete obj_field` to get rid of the entire object. – J. Titus May 09 '17 at 12:24
  • If these are in an array there are much simpler ways to do this. Please provide a [mcve] – charlietfl May 09 '17 at 12:24
  • @CarstenLøvboAndersen I get to know after 4 hours work that I need to loop through to delete obj . now I get the object Which I want to delete but Its deleting values not full object. returning Object {} as I mentioned in question. – Tousif Noor May 09 '17 at 12:26
  • @TousifNoor You would help yourself if you created a working example of your code. – Carsten Løvbo Andersen May 09 '17 at 12:27
  • @J.Titus its doing nothing . returning first state. – Tousif Noor May 09 '17 at 12:27
  • @CarstenLøvboAndersen I wish I could. I am working on part of project. Dont have access to database Just getting in this form data. And I have to do is delete object completely. – Tousif Noor May 09 '17 at 12:28
  • You're not returning anything. What do you mean it's "returning first state?" Please create a working example. – J. Titus May 09 '17 at 12:29
  • @J.Titus Its doing nothing. I mean giving result as it was. – Tousif Noor May 09 '17 at 12:32
  • You have the data and you have the code, it's poppycock that you can't create a working example. The data doesn't have to be everything, just a subset. – George May 09 '17 at 12:36
  • @George I think like this http://jsfiddle.net/DKBN6/16/ – Tousif Noor May 09 '17 at 12:39
  • @TousifNoor Have you tried something simple like http://jsfiddle.net/ns3gtdng/ ? – Carsten Løvbo Andersen May 09 '17 at 12:44
  • @CarstenLøvboAndersen its returning empty arrays. I selected 12 items and got 12 empty arrays. – Tousif Noor May 09 '17 at 12:47
  • @TousifNoor Dude, I can't promise that it all works just by copy pasting my code into your project since you can't provide us with more information then its kinda hard to make your code work. you fiddle even have errors in the console. – Carsten Løvbo Andersen May 09 '17 at 12:49
  • @CarstenLøvboAndersen Yes Now I get result but not in this format. simple 2 lines Malaing address and APN. how can I get in same formate like Array of object – Tousif Noor May 09 '17 at 13:38

3 Answers3

0
 (function filter(obj_field) {
   $.each(obj_field, function(key, value) {
     if(isEmpty(value)) {
      delete obj_field.key;
     } else if (Object.prototype.toString.call(value) === '[object Object]') 
     {
        filter(value);
     } else if ($.isArray(value)) {
          $.each(value, function(k, v) {
           filter(v);
      });
        }
       });
   })(obj_field);

   function isEmpty(obj) {
      for(var key in obj) {
         if(obj.hasOwnProperty(key))
             return false;
      }
     return true;
     }
0

Using the delete operator deletes only a reference, it will not delete the object itself.

var myObj = {}
myObj.x = 30;

console.log(myObj.x); // '30'

delete myObj; // It will not delete the object
console.log(myObj.x); // '30'

delete myObj.x; // it will delete the property of the object 
console.log(myObj.x); // 'undefined'
Rahul Kumar
  • 154
  • 9
0

Lets beautify the whole thing a bit:

mainObj={
 Object1: {"MAILING ADDRESS": "P O BOX 59", APN: "066-102-11-1"},
 Object2: {"MAILING ADDRESS": "", APN: ""},
 Object3: {"MAILING ADDRESS": "P O BOX 3", APN: "066-105-11-1"},
 Object4: {"MAILING ADDRESS": "", APN: ""} 
};

for(key in mainObj){
  if(!Object.values(mainObj[key]).some(el=>el)){
     delete mainObj[key];
  }
}

This will create this:

mainObj={
  Object1,Object3
};

http://jsbin.com/qazepupupi/edit?console

Note: this ES6, you may use the code below if this code runs on old clientside browsers:

for(key in mainObj){
  if(!Object.keys(mainObj[key]).some(function(inlinekey){ return mainObj[key][inlinekey]})){
     delete mainObj[key];
  }
}
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151