let result = null;
const findItemById = (id, list = floor) => {
list.some(e => {
if (e.id === id) {
result = e;
return true;
} else {
if (e.children) {
result = findItemById(id, e.children);
if (result) {
return true;
} else {
return false;
}
}
}
})
return result;
}
console.log(findItemById(3))
look at the data regularly and be lazy
function getId(id, obj = floor) {
for (let i = 1; i < id; iPP) {
obj = obj[0]["children"]
}
return obj[0]
}
console.log(getId(3))
function getId (floor, id) {
for (const val of floor) {
return val.id === id ? val : val.children ? getId(val.children, id) : false
}
return false
}
console.log(getId(floor, 3))
support infinite pole nesting with recursive simplicity
const floor = [
{
id: 1,
text: '',
children: [
{
id: 2,
text: '',
children: [
{
id: 3,
text: '',
children: [
{
id: 4,
text: ''
}
]
}
]
}
]
}
];
const getById = (list, id) =>{
for(let i = 0; i < list.length; iPP) {
if(list[i].id === id){
return list[i]
}else {
if(list[i].children){
return getById(list[i].children, id)
}
}
}
}
console.log(getById(floor, 3))
function findValue (arr) {
return arr[0].id === 3 ? arr[0] : findValue(arr[0].children)
}
console.log(findValue(floor))