46

I have a json array which looks something like this:

  {
    "id": 1,
    "children": [
    {
        "id": 2,
        "children": {
            "id": 3,
            "children": {
                "id": 4,
                "children": ""
            }
        }
    },
    {
        "id": 2,
        "children": {
            "id": 3,
            "children": {
                "id": 4,
                "children": ""
            }
        }
    },
    {
        "id": 2,
        "children": {
            "id": 3,
            "children": {
                "id": 4,
                "children": ""
            }
        }
    },
    {
        "id": 2,
        "children": {
            "id": 3,
            "children": {
                "id": 4,
                "children": ""
            }
        }
    },
    {
        "id": 2,
        "children": {
            "id": 3,
            "children": {
                "id": 4,
                "children": ""
            }
        }
    },
    {
        "id": 2,
        "children": {
            "id": 3,
            "children": {
                "id": 4,
                "children": ""
            }
        }
    },
    {
        "id": 2,
        "children": {
            "id": 3,
            "children": {
                "id": 4,
                "children": ""
            }
        }
    }]
}

I would like to have a function which removes the elements which has the "children" empty. How can I do it? I am not asking for the answer, only suggestions

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Stack Overfolow
  • 671
  • 1
  • 8
  • 19

2 Answers2

71

To iterate through the keys of an object, use a for .. in loop:

for (var key in json_obj) {
    if (json_obj.hasOwnProperty(key)) {
        // do something with `key'
    }
}

To test all elements for empty children, you can use a recursive approach: iterate through all elements and recursively test their children too.

Removing a property of an object can be done by using the delete keyword:

var someObj = {
    "one": 123,
    "two": 345
};
var key = "one";
delete someObj[key];
console.log(someObj); // prints { "two": 345 }

Documentation:

Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
  • 11
    +1 ... I've been coding in Javascript for years and I didn't even know the [delete](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/delete) operator existed?! – Emissary Mar 16 '13 at 16:07
  • 3
    I think he should use the splice() method rather than delete since the children are held in an array? – magritte Mar 16 '13 at 16:18
  • @magritte He has listen both arrays and objects. I'm waiting for some clarification, but yes, if there are arrays he can use `splice` to remove elements or use [`filter`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter) to iterate and remove stuff. – Lekensteyn Mar 16 '13 at 16:21
  • 3
    +1 it is the place to clear out that delete wont delete an array element from array but replace it with undefined. delete is only for deleting props from object. – Royi Namir Mar 16 '13 at 16:26
2

JSfiddle

function deleteEmpty(obj){
        for(var k in obj)
         if(k == "children"){
             if(obj[k]){
                     deleteEmpty(obj[k]);
             }else{
                   delete obj.children;
              } 
         }
    }

for(var i=0; i< a.children.length; i++){
 deleteEmpty(a.children[i])
}
Anoop
  • 23,044
  • 10
  • 62
  • 76