Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

javascript - How to remove empty object in array?

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?

question from:https://stackoverflow.com/questions/66063872/how-to-remove-empty-object-in-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...