Remove item from nested structure [duplicate]
up vote
-1
down vote
favorite
This question already has an answer here:
remove object from nested array
4 answers
I have the following object:
{
id: 1,
children: [
{id: 2},
{id: 3, children: [
{id: 4 }
]}
]
}
I want to be able to remove a specific object by using the id
property (e.g. remove the object with an id of 4). I can obviously find the object by using arr.findIndex
but how do I remove it from the enclosing array? I'm struggling with finding how to get the parent array so I can remove it. Again I know how to remove from an array using splice
but the problem I'm having is how to do this programmatically so I can remove any item from anywhere in a nested structure as above.
javascript
marked as duplicate by ponury-kostek, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
-1
down vote
favorite
This question already has an answer here:
remove object from nested array
4 answers
I have the following object:
{
id: 1,
children: [
{id: 2},
{id: 3, children: [
{id: 4 }
]}
]
}
I want to be able to remove a specific object by using the id
property (e.g. remove the object with an id of 4). I can obviously find the object by using arr.findIndex
but how do I remove it from the enclosing array? I'm struggling with finding how to get the parent array so I can remove it. Again I know how to remove from an array using splice
but the problem I'm having is how to do this programmatically so I can remove any item from anywhere in a nested structure as above.
javascript
marked as duplicate by ponury-kostek, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This question already has an answer here:
remove object from nested array
4 answers
I have the following object:
{
id: 1,
children: [
{id: 2},
{id: 3, children: [
{id: 4 }
]}
]
}
I want to be able to remove a specific object by using the id
property (e.g. remove the object with an id of 4). I can obviously find the object by using arr.findIndex
but how do I remove it from the enclosing array? I'm struggling with finding how to get the parent array so I can remove it. Again I know how to remove from an array using splice
but the problem I'm having is how to do this programmatically so I can remove any item from anywhere in a nested structure as above.
javascript
This question already has an answer here:
remove object from nested array
4 answers
I have the following object:
{
id: 1,
children: [
{id: 2},
{id: 3, children: [
{id: 4 }
]}
]
}
I want to be able to remove a specific object by using the id
property (e.g. remove the object with an id of 4). I can obviously find the object by using arr.findIndex
but how do I remove it from the enclosing array? I'm struggling with finding how to get the parent array so I can remove it. Again I know how to remove from an array using splice
but the problem I'm having is how to do this programmatically so I can remove any item from anywhere in a nested structure as above.
This question already has an answer here:
remove object from nested array
4 answers
javascript
javascript
asked Nov 8 at 9:26
Mike Rifgin
3,054135598
3,054135598
marked as duplicate by ponury-kostek, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by ponury-kostek, Nina Scholz
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:44
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Try following
let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};
/* o is object or sub-object
* k is id to be deleted
* p is parent of object, same as o for first time
* index is the index of item in the children array */
function removeKey(o, k, p=o, index) {
if(o.id === k) { // if this is the object that needs to be removed
// For first level object, make the object empty
if(o.id === p.id) {delete o.children; delete o.id}
// For others remove it from the children array
else p.children.splice(index,1);
} else if(o.children) { // if the object is not a match and has children
// iterate over the children and check and remove key
for (let i = 0; i < o.children.length; i++) {
if(removeKey(o.children[i], k, o, i)) break;
}
}
}
removeKey(obj, 4);
console.log(obj);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Try following
let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};
/* o is object or sub-object
* k is id to be deleted
* p is parent of object, same as o for first time
* index is the index of item in the children array */
function removeKey(o, k, p=o, index) {
if(o.id === k) { // if this is the object that needs to be removed
// For first level object, make the object empty
if(o.id === p.id) {delete o.children; delete o.id}
// For others remove it from the children array
else p.children.splice(index,1);
} else if(o.children) { // if the object is not a match and has children
// iterate over the children and check and remove key
for (let i = 0; i < o.children.length; i++) {
if(removeKey(o.children[i], k, o, i)) break;
}
}
}
removeKey(obj, 4);
console.log(obj);
add a comment |
up vote
1
down vote
Try following
let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};
/* o is object or sub-object
* k is id to be deleted
* p is parent of object, same as o for first time
* index is the index of item in the children array */
function removeKey(o, k, p=o, index) {
if(o.id === k) { // if this is the object that needs to be removed
// For first level object, make the object empty
if(o.id === p.id) {delete o.children; delete o.id}
// For others remove it from the children array
else p.children.splice(index,1);
} else if(o.children) { // if the object is not a match and has children
// iterate over the children and check and remove key
for (let i = 0; i < o.children.length; i++) {
if(removeKey(o.children[i], k, o, i)) break;
}
}
}
removeKey(obj, 4);
console.log(obj);
add a comment |
up vote
1
down vote
up vote
1
down vote
Try following
let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};
/* o is object or sub-object
* k is id to be deleted
* p is parent of object, same as o for first time
* index is the index of item in the children array */
function removeKey(o, k, p=o, index) {
if(o.id === k) { // if this is the object that needs to be removed
// For first level object, make the object empty
if(o.id === p.id) {delete o.children; delete o.id}
// For others remove it from the children array
else p.children.splice(index,1);
} else if(o.children) { // if the object is not a match and has children
// iterate over the children and check and remove key
for (let i = 0; i < o.children.length; i++) {
if(removeKey(o.children[i], k, o, i)) break;
}
}
}
removeKey(obj, 4);
console.log(obj);
Try following
let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};
/* o is object or sub-object
* k is id to be deleted
* p is parent of object, same as o for first time
* index is the index of item in the children array */
function removeKey(o, k, p=o, index) {
if(o.id === k) { // if this is the object that needs to be removed
// For first level object, make the object empty
if(o.id === p.id) {delete o.children; delete o.id}
// For others remove it from the children array
else p.children.splice(index,1);
} else if(o.children) { // if the object is not a match and has children
// iterate over the children and check and remove key
for (let i = 0; i < o.children.length; i++) {
if(removeKey(o.children[i], k, o, i)) break;
}
}
}
removeKey(obj, 4);
console.log(obj);
let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};
/* o is object or sub-object
* k is id to be deleted
* p is parent of object, same as o for first time
* index is the index of item in the children array */
function removeKey(o, k, p=o, index) {
if(o.id === k) { // if this is the object that needs to be removed
// For first level object, make the object empty
if(o.id === p.id) {delete o.children; delete o.id}
// For others remove it from the children array
else p.children.splice(index,1);
} else if(o.children) { // if the object is not a match and has children
// iterate over the children and check and remove key
for (let i = 0; i < o.children.length; i++) {
if(removeKey(o.children[i], k, o, i)) break;
}
}
}
removeKey(obj, 4);
console.log(obj);
let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};
/* o is object or sub-object
* k is id to be deleted
* p is parent of object, same as o for first time
* index is the index of item in the children array */
function removeKey(o, k, p=o, index) {
if(o.id === k) { // if this is the object that needs to be removed
// For first level object, make the object empty
if(o.id === p.id) {delete o.children; delete o.id}
// For others remove it from the children array
else p.children.splice(index,1);
} else if(o.children) { // if the object is not a match and has children
// iterate over the children and check and remove key
for (let i = 0; i < o.children.length; i++) {
if(removeKey(o.children[i], k, o, i)) break;
}
}
}
removeKey(obj, 4);
console.log(obj);
answered Nov 8 at 9:37
Nikhil Aggarwal
22.9k32647
22.9k32647
add a comment |
add a comment |