title: Converting existing Array into a new one
body: <p>I have a Array that I am turning into a new one that's suitable for my work. This is the original ArrayI have:</p>
  
  <p>Note: 'jayanagar', 'mallashwaram', 'kolar' are referred as city in my code.</p>
  
  <pre><code>var array1=[
    ["Year", "2018-8", "2017-8"],
    ["JAYANAGAR", "2018-8", 10910665], 
    ["MALLESHWARAM", "2018-8", 2018451],
    ["KOLAR", "2018-8", 2277562], 
    ["JAYANAGAR", "2017-8", 1134]
  ]
  </code></pre>
  
  <p>And from this I am converting it into this:</p>
  
  <pre><code>var array2=[
    ["Year", "Jul 2018", "Jul 2017"], 
    ["JAYANAGAR", 10910665, 1134], 
    ["MALLESHWARAM", 2018451], 
    ["KOLAR", 2277562]
  ]
  </code></pre>
  
  <p>What I am trying to do is</p>
  
  <ul>
  <li><p>As you can see in <code>arra1</code> I have two years, i.e <code>2018-8</code> and <code>2017-8</code>, for date <code>2018-8</code> i have all the city's  data but as for date <code>2017-08</code>  have only one city data, i.e for <code>jayanagar</code></p></li>
  <li><p>So what I am trying to do is whenever there is less number of cities for previous date, I want to show as <code>0</code> for that city</p></li>
  <li><p>For example in <code>array2</code>, I don't have data for <code>malleshwaram</code> and <code>kolar</code> so I want to show as 0 there</p></li>
  <li><p>And the year user selected will give me all the names of cities, so according to that i want to loop over the other date data here, that one is <code>array2</code></p></li>
  <li><p>The highest date is always going to have all the cities name </p></li>
  </ul>
  
  <p><strong>What i have done</strong></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 input = [
    ["Year", "2018-8", "2017-8"],
    ["JAYANAGAR", "2018-8", 10910665],
    ["MALLESHWARAM", "2018-8", 2018451],
    ["KOLAR", "2018-8", 2277562],
    ["JAYANAGAR", "2017-8", 1134]
  ]
  var e = input[0]
  var a = new Date(e[1]);
  
  a.setMonth(a.getMonth());
  a = a.toUTCString();
  
  var c = a.split(' ');
  e[1] = c[2] + " " + c[3];
  a = new Date(e[2]);
  
  a.setMonth(a.getMonth());
  a = a.toUTCString();
  c = a.split(' ');
  e[2] = c[2] + " " + c[3];
  
  const merged = input.reduce((acc, arr) =&gt; {
    const [city, year, value] = arr;
  
    if (city === "Year")
      acc[city] = arr
    else {
      acc[city] = acc[city] || [city]
      acc[city].push(value)
    }
  
    return acc;
  }, {})
  
  const output = Object.values(merged)
  console.log(output)</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>My expected output</strong></p>
  
  <pre><code>[
    [
      "Year",
      "Aug 2018",
      "Aug 2017"
    ],
    [
      "JAYANAGAR",
      10910665,
      1134
    ],
    [
      "MALLESHWARAM",
      2018451,
      0
    ],
    [
      "KOLAR",
      2277562,
      0
    ]
  ]
  </code></pre>