How to go deep into the object?

  • 0
    There is an object of the form
    const someObj = {
    title: 1,
    children: {
    title: 2,
    children: {
    title: 3,
    }
    }
    }


    You need to return the object that does not have a children property. How to do it correctly? Thank you.
    JavaScript Anonymous, Jul 14, 2020

  • 2 Answers
  • 0
    const getLastChild = (obj) => obj.children ? getLastChild(obj.children) : obj;
    Anonymous

  • 0
    const someObj = {
    title: 1,
    children: {
    title: 2,
    children: {
    title: 3,
    }
    }
    }

    function getNestedObject(obj) {
    if (obj.children) {
    return getNestedObject(obj.children)
    }

    return obj.title
    }

    console.log(getNestedObject(someObj)) // 3
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!