title: Change Object Structure and Properties in Javascript
body: <p>I am facing a particular problem that I need to change the structure of a object like:</p>
  
  <pre><code>customerPayload = {
        name: 'Name must remain',
        billing_address_street: 'Electric Avenue',
        billing_address_number: 222,
        shipping_address_street: 'Avenue 1',
        shipping_address_number: 1337
      };
  </code></pre>
  
  <p>into this: </p>
  
  <pre><code>customerPayload = {
          name: 'Name must remain',
          billing_address: {
              street: 'Electric Avenue',
              number: 222
          },
          shipping_address: {
              street: 'Avenue 1',
              number: 1337
          }
      };
  </code></pre>
  
  <p>I tried something like this:</p>
  
  <pre><code>for (var p in customerPayload) {
          if (customerPayload.hasOwnProperty(p)) {
              if (p.includes('billing')) {      
                  var b = p.substr(0, 15) + '.' + p.substr(15 + 1);           
                  var bAddress = b.split('.')[0];
                  var childProp = b.split('.')[1];
                  newCustomerPayload[bAddress] = {
                    [childProp]: customerPayload[p]
                  };
              }
              // else if shipping ... same thing
          }
      }
  </code></pre>
  
  <p>But the result is an object with only the last changed properties:</p>
  
  <pre><code> customerPayload = {
      billing_address: {
          number: 222
      },
      shipping_address: {
          street: 'Avenue 1'
      }
   };
  </code></pre>
  
  <p>Please, any help?</p>