title: Reduce Array of objects to object of objects
body: <p>I want to convert an Array of objects to Object of Objects.</p>
  
  <p>My data: </p>
  
  <pre><code>var pools = [{
      dce: 3,
      lts: 2,
      name: "nift nation",
    },
    {
      dce: 1049.99,
      lts: 104.999,
      name: "NSG I.NS. Mark Select",
    },
    {
      dce: 162,
      lts: 36.157,
      name: "Shift-Team Mark Select",
    }
  ]
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>{
    nift_nation: {
      nift_nationDollars: "",
      nift_nationUnits: "",
      nift_nationPercentage: ""
    },
    NSG_I$NS$_Mark_Select: {
      NSG_I$NS$_Mark_SelectDollars: "",
      NSG_I$NS$_Mark_SelectUnits: "",
      NSG_I$NS$_Mark_SelectPercentage: ""
    },
    Shift__Team_Mark_Select: {
      Shift__Team_Mark_SelectDollars: "",
      Shift__Team_Mark_SelectUnits: "",
      Shift__Team_Mark_SelectPercentage: ""
    }
  }
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var pools = [{
      dce: 3,
      lts: 2,
      name: "nift nation",
    },
    {
      dce: 1049.99,
      lts: 104.999,
      name: "NSG I.NS. Mark Select",
    },
    {
      dce: 162,
      lts: 36.157,
      name: "Shift-Team Mark Select",
    }
  ]
  
  var suffixArray = ["Dollars", "Percentage", "Units"];
  
  var getFieldSuffix = function(rowFieldCount) {
    switch (rowFieldCount) {
      case 0:
        return 'Dollars';
      case 1:
        return 'Units';
      case 2:
        return 'Percentage';
      default:
        return
    }
  };
  
  var replacementMap = {
    single_space: '_',
    dot: '$',
    hyphen: '__',
  };
  
  var replacer = function(str) {
    return str.replace(/[ .-]/g, l =&gt; {
      if (l == ".") return replacementMap["dot"];
      if (l == " ") return replacementMap["single_space"];
      return replacementMap["hyphen"];
    });
  };
  
  var formStructure = function dataFormatter(collection, suffixArr) {
    const data = collection.map(pool =&gt; Object.assign({
      [replacer(pool.name)]: suffixArr.reduce((acc, suffix, index) =&gt; {
        acc[replacer(pool.name) + getFieldSuffix(index % 3)] = '';
        return acc;
      }, {}),
    }));
  
    return Object.assign({}, ...data); //Extra step, I don't think this is the best way
  }
  
  var arrObj = formStructure(pools, suffixArray); 
  
  console.log(arrObj)</code></pre>
  </div>
  </div>
  </p>
  
  <p>I get the desired output. In <code>formStructure</code> function I store the result that is an <strong>Array of Objects</strong> in variable <code>data</code>, then in the next step <code>return Object.assign({}, ...data);</code>, I convert it into <strong>Object of objects</strong>. This approach is not optimum.</p>
  
  <p>I want to be able to get <strong>Object of objects</strong> in variable <code>data</code> itself.</p>