I'm looping through a nested object of objects, looking for a specific object and if I'm I find it, I do stuff. I can get it working for the first nest, but any nest after that I get an undefined value.
let myObj = [{
id: 1,
children: [{
id: 1.1,
children: []
},
{
id: 1.2,
children: []
}
]
},
{
id: 2,
children: [{
id: 2.1,
children: []
},
{
id: 2.2,
children: []
}
]
}
]
function addToObj(itemToAdd, parentId, obj) {
for (let i = 0; i < obj.length; i++) {
const item = search(obj[i], parentId);
console.log(item); // undefined
if (item) {
item.children = item.children.concat(itemToAdd);
break;
}
}
function search(obj, id) {
if (obj.id === id) {
console.log(obj); // defined (obj with id of 2.1), but returns undefined?
return obj;
}
for (let i = 0; i < obj.children.length; i++) {
search(obj.children[i], id);
}
}
return obj;
};
const itemToAdd = {
id: 100,
}
addToObj(itemToAdd, 2.1, myObj);
The function in the above snippet loops through the object, looking for a specific item. If it finds the item it will insert an object into that items children property.
2 Answers 2
You need to use the return value from the recursive search: if it exists, return it:
for (let i = 0; i < obj.children.length; i++) {
const possibleResult = search(obj.children[i], id);
if (possibleResult) {
return possibleResult;
}
}
let myObj = [{
id: 1,
children: [{
id: 1.1,
children: []
},
{
id: 1.2,
children: []
}
]
},
{
id: 2,
children: [{
id: 2.1,
children: []
},
{
id: 2.2,
children: []
}
]
}
]
function addToObj(itemsToAdd, parentId, obj) {
for (let i = 0; i < obj.length; i++) {
const item = search(obj[i], parentId);
// first log here will be undefined, nothing found
// second log here will find the object
console.log('item', item);
if (item) {
item.children = item.children.concat(itemsToAdd);
break;
}
}
function search(obj, id) {
if (obj.id === id) {
console.log('obj', obj); // defined (obj with id of 2.1), but returns undefined?
return obj;
}
for (let i = 0; i < obj.children.length; i++) {
const possibleResult = search(obj.children[i], id);
if (possibleResult) {
return possibleResult;
}
}
}
return obj;
};
const itemToAdd = {
id: 100,
}
addToObj(itemToAdd, 2.1, myObj);
answered Mar 17, 2019 at 7:32
CertainPerformance
374k55 gold badges354 silver badges359 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
There are two problems in code
- if
(obj.id === id)isfalsethen in loop you are returning nothing. - You should check if
obj.childrenexists before loop.
let myObj = [
{
id: 1,
children: [
{
id: 1.1,
children: []
},
{
id: 1.2,
children: []
}
]
},
{
id: 2,
children: [
{
id: 2.1,
children: []
},
{
id: 2.2,
children: []
}
]
}
]
There are two problems in code:
- List item
function addToObj(itemToAdd, parentId, obj) {
for (let i=0;i<obj.length;i++) {
const item = search(obj[i], parentId);
console.log(item); // undefined
if (item) {
item.children = item.children.concat(itemToAdd);
break;
}
}
function search(obj, id) {
if (obj.id === id) {
console.log(obj); // defined (obj with id of 2.1), but returns undefined?
return obj;
}
if(obj.children){
for (let i=0;i<obj.children.length;i++) {
let x = search(obj.children[i], id);
if(x) return x;
}
}
}
return obj;
};
const itemToAdd = {
id: 100,
}
addToObj(itemToAdd, 2.1, myObj);
answered Mar 17, 2019 at 7:43
Maheer Ali
36.5k8 gold badges53 silver badges83 bronze badges
Comments
lang-js
if (obj.id === id)is false, there is no return value