Answer the question
In order to leave comments, you need to log in
How to move a child object?
There is an array of objects:
const boxes = [
{
id: 1,
inner: [
{
id: 2,
inner: [
{
id: 3,
inner: []
},
{
id: 4,
inner: []
}
]
},
{
id: 5,
inner: []
}
]
}]
const boxes = [
{
id: 1,
inner: [
{
id: 2,
inner: [
{
id: 4,
inner: []
}
]
},
{
id: 5,
inner: []
},
{
id: 3,
inner: []
},
]
}]
Answer the question
In order to leave comments, you need to log in
Added the delete function and rewrote the search (now the functions are clean)
//GET
const findEl = (ar, id) => {
return ar.reduce((a, el) => {
if (a !== null)
return a
return el.id === id ? el : findEl(el.inner, id)
}, null)
}
//REMOVE
const removeEl = (ar) => {
for (let i = 0; i < ar.length; i++) {
if (ar[i].inner.length)
removeEl(ar[i].inner)
if (ar[i].id === action.box_id)
delete ar[i]
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question