0

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.

1
  • 1
    when if (obj.id === id) is false, there is no return value Commented Mar 17, 2019 at 7:32

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

Comments

1

There are two problems in code

  • if (obj.id === id) is false then in loop you are returning nothing.
  • You should check if obj.children exists 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.