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

7
  • Careful: those = signs hint at invalid JSON Commented Mar 16, 2013 at 15:56
  • 2
    @magritte stop with this redundant question. he said he is asking for suggestions. not the answer/ Commented Mar 16, 2013 at 15:57
  • @rjz , yes those = should be : ... Commented Mar 16, 2013 at 15:59
  • i know that is not a valid json ... i just wanted to give an example of how it looks. Commented Mar 16, 2013 at 16:01
  • 1
    If you say you've got an array, please post an array. And check out the filter array function Commented Mar 16, 2013 at 16:19

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

4 Comments

+1 ... I've been coding in Javascript for years and I didn't even know the delete operator existed?!
I think he should use the splice() method rather than delete since the children are held in an array?
@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 to iterate and remove stuff.
+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.
3

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])
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.