I am trying to delete an object from a JSON object. Is there an easy way to do it? If I have an object can I delete the item from it, or is it better to convert the object into an array and splice it? What are the pros and cons of each.
The logic I want to write is something like this.
function remove( delKey, delVal, o, stack){
for(var key in o) {
if(typeof o[key] === "object") {
stack != undefined ? stack += "." + key : stack = key;
remove(delKey, delVal, o[key], stack);
} else {
if(delKey == key && delVal == o[key]) {
delete o;
}
}
}
}
changed code to use delete instead of splice
So this is basically what I would like to do, if there is an easier way then please let me know. The problem I am having here is A. I do not know where to splice. B. if I do splice, I think I am not going to return the spliced result through the recursion to the other objects.
My issue is since I will have different JSON every time I do not know the nested properties. That is why I am using the stack variable. Stack will the nested properties. SO if I want to delete say the color of an apple, stack will be json.fruit.apple.color. But it is a string not an object.
Anyways, does anyone have a better solution for deleting an object from JSON?
I am trying to delete an object from a JSON object. Is there an easy way to do it? If I have an object can I delete the item from it, or is it better to convert the object into an array and splice it? What are the pros and cons of each.
The logic I want to write is something like this.
function remove( delKey, delVal, o, stack){
for(var key in o) {
if(typeof o[key] === "object") {
stack != undefined ? stack += "." + key : stack = key;
remove(delKey, delVal, o[key], stack);
} else {
if(delKey == key && delVal == o[key]) {
delete o;
}
}
}
}
changed code to use delete instead of splice
So this is basically what I would like to do, if there is an easier way then please let me know. The problem I am having here is A. I do not know where to splice. B. if I do splice, I think I am not going to return the spliced result through the recursion to the other objects.
My issue is since I will have different JSON every time I do not know the nested properties. That is why I am using the stack variable. Stack will the nested properties. SO if I want to delete say the color of an apple, stack will be json.fruit.apple.color. But it is a string not an object.
Anyways, does anyone have a better solution for deleting an object from JSON?
Share Improve this question edited May 12, 2014 at 22:13 Robert Harvey 181k48 gold badges348 silver badges513 bronze badges asked May 31, 2013 at 22:25 recnepsrecneps 1,2955 gold badges16 silver badges27 bronze badges 4-
5
You're looking for the
delete
operator. – SLaks Commented May 31, 2013 at 22:27 - Sure, ok I have tried that as well. but if I just try to delete o; it does not work. – recneps Commented May 31, 2013 at 22:28
-
2
You also need to learn how to use the
delete
operator. See MDN's docs. – SLaks Commented May 31, 2013 at 22:36 - 1 Please don't put your answer in the question. You are allowed to post answers to your own question and accept them. – Dennis Commented May 31, 2013 at 23:06
2 Answers
Reset to default 4You can use the delete
operator: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/delete
Hopefully this helps someone else from struggling with this. Thanks guys for the help.
Here is the answer and the function that I have that works for me. It is abstract enough to hopefully help someone else out.
If you pass this function any key value pair you want to delete and the parsed JSON object it will delete it and return true if the item was found and deleted.
function remove(delKey, delVal, o) {
for (var key in o) {
if (typeof o[key] === "object") {
if (remove(delKey, delVal, o[key])) { return true; }
} else {
if (delKey == key && delVal == o[key]) {
delete o[key];
return true;
}
}
}
}