title: Restructuring Javascript Arrays
body: <p><strong>EDIT: Thanks for the replies!</strong></p>
  
  <p>I am kind of new to working with Javascript (sorry for probably the bad code). I've mainly only worked with HTML and CSS. I have been trying to experiment with Javascript, but I've been stuck on this forever. The outcome is not what I want it to be. </p>
  
  <p><strong>My code:</strong></p>
  
  <p>I want to make the array easier to use, by putting the values under the same category, but my code's output is not exactly what I want it to be like.</p>
  
  <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 data = {
    idName: "idName",
    valueRanges: [{
      range: "range",
      majorDimension: "ROWS",
      values: [
        [
          "ID",
          "Category",
          "Name"
        ],
        [
          "1",
          "Category1",
          "Name1"
        ],
        [
          "2",
          "Category1",
          "Name2"
        ],
        [
          "3",
          "Category2",
          "Name3"
        ],
        [
          "4",
          "Category1",
          "Name4"
        ]
      ]
    }]
  }
  
  var rows = [];
  
  let batchRowValues = data.valueRanges[0].values
  for (let i = 1; i &lt; batchRowValues.length; i++) {
    let rowObject = {};
    for (let j = 0; j &lt; batchRowValues[i].length; j++) {
      rowObject[batchRowValues[0][j]] = batchRowValues[i][j];
    }
    rows.push(rowObject);
  }
  
  var newArray = rows.reduce(function(acc, curr) {
    var findIfNameExist = acc.findIndex(function(item) {
      return item.Category === curr.Category;
    })
    if (findIfNameExist === -1) {
      let obj = {
        'Category': curr.Category,
        'value': [curr]
      }
      acc.push(obj)
    } else {
      acc[findIfNameExist].value.push(curr)
    }
    return acc;
  }, []);
  
  console.log('------------')
  console.log('input: ' + JSON.stringify(data, null, 2))
  console.log('------------')
  console.log('output: ' + JSON.stringify(newArray, null, 2))</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>My code's output:</strong></p>
  
  <pre><code>[
    {
      Category: "Category1",
      value: [
        {
          Category: "Category1",
          ID: "1",
          Name: "Name1"
        }, 
        {
          Category: "Category1",
          ID: "2",
          Name: "Name2"
        },
        {
          Category: "Category1",
          ID: "4",
          Name: "Name4"
        }
      ]
    },
    {
      Category: "Category2",
      value: [
        {
          Category: "Category2",
          ID: "3",
          Name: "Name3"
        }
      ]
    }
  ]
  </code></pre>
  
  <p><strong>How I want it to look:</strong></p>
  
  <pre><code>[
    {
      Category: "Category1",
      values: [
        {
          ID: "1",
          Name: "Name1"
        },
        {
          ID: "2",
          Name: "Name2"
        },
        {
          ID: "4",
          Name: "Name4"
        }
      ]
    },
    {
      Category: "Category2",
      values: [
        {
          ID: "3",
          Name: "Name3"
        },
      ]
    },
  ]
  </code></pre>
  
  <p>I want to learn! I appreciate any help. </p>