2

I have list of objects whom I want to loop through to see for the value of a key, in this I want to remove objects whode id= "null". Below is snippet:

obj: {
     after:{
        id: null,
        name:'test',
        address: '63784hgj'
     },
      before:{
        id: 678346,
        name:'test',
        address: '63784hgj'
     },
      single:{
        id: null,
        name:'test',
        address: '63784hgj'
     },
      range:{
        id: 65847,
        name:'test',
        address: '63784hgj'
     }
 }

I tried some sloppy way of doing this, considering I know the object that has id with null value:

obj.abc.id.destroy;
obj.abc3.id.destroy;

below is the way the object is seen: enter image description here

is there any way I could remove objects that dont have null values for their id's?

Thx~

Community
  • 1
  • 1
user1234
  • 3,000
  • 4
  • 50
  • 102
  • 1
    You're asking how to delete a property of an object? –  Jun 22 '16 at 23:57
  • no delete the whole object if the value of a prop is null – user1234 Jun 22 '16 at 23:59
  • Pretty sure you can do ```delete obj.single``` in this case. if you wanted to loop, maybe loop ```var prop in obj``` and ```if (obj[prop].id === null)```, you ```delete obj[prop]``` – vtange Jun 23 '16 at 00:00
  • But the object you want to delete is on a property of another object, right? –  Jun 23 '16 at 00:00
  • 1
    Do you want to delete ```obj``` or delete ```single```? – vtange Jun 23 '16 at 00:00
  • I would want to delete single and after(both objects) as thay have id's with null values – user1234 Jun 23 '16 at 00:01
  • There is something inherently wrong with your app logic if it allows such situation and this situation requires deleting the object. – Azamantes Jun 23 '16 at 00:01
  • @user1234 then you want to do the loop method I just wrote an answer for – vtange Jun 23 '16 at 00:08

3 Answers3

2

If you want to just delete single: delete obj.single;

If you want to delete multiple objects within obj:

for (var prop in obj){
    if (obj[prop].id === null){
        delete obj[prop];
    }
}
vtange
  • 649
  • 1
  • 5
  • 10
1

you can use the delete operator.

e.g.

for (var prop in obj) { 
    var item = obj[prop];  
    if (!item.id) {
        delete item; 
    }
}
Dan
  • 774
  • 5
  • 11
  • how about for deleting multiple object in one func? I'd need to remove the single and after object coz both of them have prop with null values – user1234 Jun 23 '16 at 00:00
  • just loop through each item in the object – Dan Jun 23 '16 at 00:02
1

I think these are simpler answers than above (with respect to line of code):

var obj= {
     after:{ id: null, name:'test', address: '63784hgj' },
      before:{ id: 678346, name:'test', address: '63784hgj' },
      single:{ id: null, name:'test', address: '63784hgj' },
      range:{ id: 65847, name:'test', address: '63784hgj' },
 };
// Solution #1: set items to new object.
var newObj = {};
Object.keys(obj)
  .forEach(k => obj[k].id && (newObj[k] = obj[k]));

// Solution #2: delete unwanted from object itself.
Object.keys(obj)
  .forEach(k => !obj[k].id && delete obj[k]);

console.log(newObj);
console.log(obj);
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48