title: simplify object transformation with javascript higher order functions
body: <p>Goal: I am trying to reduce complexity of my object transformation. I've been told to use higher order functions to reduce the complexity. I am mainly looking at the for block of code to simplify but any other pointers or help would be appreciated.</p>
  
  <p>Problem: I do not see a clear way to simplify this even with higher order functions. </p>
  
  <p>Note: that I am not guaranteed to have all the fields so i need to guard against key undefined. That is why i am doing (model) => model.key to only read property if i verify the key is there.</p>
  
  <p>model:</p>
  
  <pre><code>{ 
    "origName1": "val1",
    "origName2": "val2",
    "origName3": "val3",&lt;-- this key value is not always present
    "origName4": "val4"  
  }
  </code></pre>
  
  <p>my transformation obj:</p>
  
  <pre><code>let transFormObj = {};
  transFormObj.origName1= {'newKey': 'newName1', 'value': (model) =&gt; model.origName1};
  transFormObj.origName2= {'newKey': 'newName2', 'value': (model) =&gt; model.origName2};
  transFormObj.origName3= {'newKey': 'newName3', 'value': (model) =&gt; model.origName3};
  transFormObj.origName4= {'newKey': {parentKey: 'newObj', childKey: 'newName4'}, 'value': (model) =&gt; model.origName4};
  </code></pre>
  
  <p>The for block i am focusing on:</p>
  
  <pre><code> let returnObj = {}
          for(let key in model) {
              if(key in transFormObj) {
                  // is nested object?
                  if(typeof(transFormObj[key].newKey) === 'object') {
                      // if parent key doesn't exist create else keep it the same
                      returnObj[transFormObj[key].newKey.parentKey] = returnObj[transFormObj[key].newKey.parentKey] || {};
                      returnObj[transFormObj[key].newKey.parentKey][transFormObj[key].newKey.childKey] = transFormObj[key].value(model);
                  } else {
                  returnObj[transFormObj[key].newKey] = transFormObj[key].value(model);}
              }
          }
  </code></pre>
  
  <p>expected output object:</p>
  
  <pre><code>{ 
        "newName1": "val1",
        "newName2": "val2",
        "newName3": "val3",
        "newObj": {"newName4" :"val4"}  
      }
  </code></pre>