Removing a JavaScript property inside a specific Object within an Array

- 1 answer

Ad

I have an object that looks like this when I do a stringify():

{"key-1":{"inner_key_obj-1":{"A":"1", "AA":"11", "AAA":"111"}, "inner_key_obj-2":{"B":"2", "BB":"22", "BBB":"222"}, "inner_key_obj-3":{"C":"3", "CC":"33", "CCC":"333"}}, "key-2" : "not-an-object-property" }

I'd like to search for and remove they key inner_key_obj-2 so that the object becomes:

{"key-1":{"inner_key_obj-1":{"A":"1", "AA":"11", "AAA":"111"},  "inner_key_obj-3":{"C":"3", "CC":"33", "CCC":"333"}}, "key-2" : "not-an-object-property" }

I know I can use delete to remove a key and its value from an object, but how do I loop thru this to get there?

I did some basic tests such as this:

  for (var key in object) 
        {
            if (object.hasOwnProperty(key)) 
            {                
                //Now, object[key] is the current value
                if (object[key] == null)
                { 
                    delete object[type];
                }
            }
        }

...but to no avail. Can someone explain how to loop thru this?

Ad

Answer

Ad

Doesn't

delete object["key-1"]["inner_key_obj-2"];

do what you want?

Ad
source: stackoverflow.com
Ad