title: Return nested object with recursion - Javascript
body: <p>I have an object with nested object:</p>
  
  <pre><code>let list = {
    value: 1,
    next: {
      value: 2,
      next: {
        value: 3,
        next: {
          value: 4,
          next: null
        }
      }
    }
  };
  </code></pre>
  
  <p>I need to return all <code>key: value</code> of <code>list</code> and I must use recursion. I have tried to push the nested object to the local variable in the function, but it fails in the second iteration because the names are different.</p>
  
  <p>Here is the function:</p>
  
  <pre><code>function printList(list){
    let nested = {};
  
    if(list.hasOwnProperty('next')) {
        nested = list.next;
        printList(nested);
    } else {
      return nested;
    }
  }
  </code></pre>
  
  <p>Is there a way to solve it with recursion?</p>
  
  <p>It should return the <code>value</code> properties. In this case </p>
  
  <pre><code>1
  2
  3
  4
  </code></pre>