1

I am trying to remove the empty object {} from the below structure.

data =  [{
            "total" : "value",
            "status" : "statusVal",
            "recs" : [{
                    "total" : "value",
                    "region" : "name",
                    "recs" : [{},{
                            "recs" : [{
                                    "recs" : [{
                                            "value" : "a",
                                            "label" : "fn"
                                        }]
                                }]
                        }]
                }]
        }]

This is my JavaScript code where I process the data and trying to remove the empty object from the result.

var result = json.parse(data);
for(var i=0;i<result.length;i++){
   if(result[i].hasOwnProperty("recs")){
      var fset = result[i].recs;
      for(var j=0;j<fset.length;j++){
         if(fset[j].recs === undefined || fset[j].recs === null){
            delete fset[j].recs;
         }
         if(fset[j].hasOwnProperty("recs")){
           var sset = fset[i].recs;
             for(var k=0;k<sset.length;k++){
                var tset = sset[i].recs;
                if(sset[k].hasOwnProperty("recs")){
                   for(var z=0;z<tset.length;z++){
                      if(tset[z].hasOwnProperty("recs")){
                         //  logic to push 
                      }
                   }
                }
             }
         }
      }
   }
}

I tried checking null and undefined and also with property check bool as false. Since the empty {} is always returning length as 1, that is also ruled out. I am stuck here on processing the removal of empty object.

Above code is removing the entire recs node. Can you help me find what I am missing?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
ləːnə
  • 323
  • 2
  • 8
  • 22
  • 1
    Does this answer your question? [How to check if object has any properties in JavaScript?](https://stackoverflow.com/questions/2673121/how-to-check-if-object-has-any-properties-in-javascript) – Heretic Monkey Feb 05 '21 at 13:12

1 Answers1

1

Check the length of the Object.keys() to see if object is empty or not.

Object.keys(fset[j].recs).length === 0 

You can't iterate all the dynamic levels of array manually, so better to write the function which has recursive function call.

var data = [{
  "total": "value",
  "status": "statusVal",
  "recs": [{
    "total": "value",
    "region": "name",
    "recs": [{}, {
      "recs": [{
        "recs": [{
          "value": "a",
          "label": "fn"
        }]
      }]
    }]
  }]
}]

function removeEmpty(ary) {
  ary.forEach((item, index) => {
    if (Object.keys(item).length === 0) { ary.splice(index, 1); }
    else if (item.recs && item.recs.length > 0)
      removeEmpty(item.recs)
  });
}

removeEmpty(data)
console.log(data)
wangdev87
  • 8,611
  • 3
  • 8
  • 31
  • This seems like an obvious duplicate. The linked dupe is from 2010 and has been asked many, many times. – Heretic Monkey Feb 05 '21 at 13:17
  • 1
    yeah, i got your point, regarding this question, we need another solution, and i just updated my answer. @HereticMonkey – wangdev87 Feb 05 '21 at 13:24
  • Hi thanks for your response. Actually it takes length as 1 with the empty object. Because it returns 1 its tricky to remove the empty object. – ləːnə Feb 05 '21 at 15:37
  • 1
    can you specify in detail? i added better solution in my answer. – wangdev87 Feb 05 '21 at 15:39