title: looping through objects within an object with Object.assign
body: <p>The script below currently outputs:</p>
  
  <pre><code>{
      "title1": {
          "0": "undefined"
      },
      "title2": "&amp;amp; three",
      "title3": "&amp;amp; four"
  }
  </code></pre>
  
  <p>The desired output being: </p>
  
  <pre><code> {
        "title1": {
          "nested1": "one",
          "nested2": "&amp;amp; two"
        },
        "title2": "&amp;amp; three ",
        "title3": "&amp;amp; four"
      }
  </code></pre>
  
  <p>My question is what is required in the if statement to successfully loop through the properties of the [key][innerKey] and pass them through the encode function and return them to 'out'?</p>
  
  <p>I have been trying to solve it using a for loop within the first part of the if statement without success. I figure it needs to follow a logic along the lines of:</p>
  
  <ul>
  <li>if arr[key] is an object then</li>
  <li>output the [key]: </li>
  <li>then loop through all properties of the [key] </li>
  <li>and pass them through the function encode </li>
  <li>and return them to 'out'</li>
  </ul>
  
  <p>thanks.</p>
  
  <pre><code>var arr = {
    "title1": {
      "nested1": "one",
      "nested2": "&amp; two"
    },
    "title2": "&amp; three ",
    "title3": "&amp; four"
  };
  
  var encodedValues = Object.keys(arr).reduce(function(out,key,innerKey) {
  
    if (typeof arr[key] == "object") {
       return Object.assign(out, {[key]: {[innerKey]: encode(arr[key][innerKey])}})
  
    } else {
       return Object.assign(out, {[key]: encode(arr[key])})
    }
  
  }, {});
  
  console.log(encodedValues)
  </code></pre>