title: Javascript Recursion normalize JSON data
body: <p>I have a json response and I want to simplify this json data with recursion and reduce. I write a function put i stuck at this point. I want to remove all data element and create a new object without data name.</p>
  
  <p>My json data is like this</p>
  
  <pre><code>[
      {
          "city": {
              "data": {
                  "id": 649442,
                  "country_id": 54,
                  "state_id": 682,
                  "city_name": "Prague",
                  "state_name": "Praha"
              }
          },
          "country": {
              "data": {
                  "id": 54,
                  "data": {
                      "country_name": "Czech Republic",
                      "short_country": "CZ",
                      "olympic_code": "CZE"
                  }
              }
          }
      }
  ]
  </code></pre>
  
  <p>And my function is here:</p>
  
  <pre><code>function normalizeData(object) {
    return Object.keys(object).reduce((finalObject, objectKey) =&gt; {
      if (object[objectKey] &amp;&amp; object[objectKey].data) {
        finalObject[objectKey] = object[objectKey].data;
        if (Array.isArray(finalObject[objectKey].data)) {
          finalObject[objectKey] = object[objectKey].data.map(item =&gt; {
            return normalizeData(item);
          });
        } else {
          finalObject[objectKey] = normalizeData(object[objectKey].data);
        }
      } else {
        finalObject[objectKey] = object[objectKey];
      }
      return finalObject;
    }, {});
  };
  </code></pre>
  
  <p>At the and I am still getting data object. So where am I making mistake. Or is there a better way to do this?</p>