title: Pushing the values into an array results in undefined variables but works inside a function
body: <p>I'm trying to only push the values in the 'eachNumber' array with the indexes from the 'indexes' variable inside the 'appearMost' array, but for some reason it returns an array with undefined values:</p>
  
  <pre><code>var indexes = [1,2];
  var appearMost = [];
  var eachNumber = [4, 7, 9, 8];
  
  indexes.map(function(c) { appearMost.push(eachNumber[c]) }); // should return [7,9]
  </code></pre>
  
  <p>The result of appearMost should be [7,9].</p>
  
  <p>Strange, because I've built a function that returns the number appearing most frequently in an array which relies on the above line that doesn't seem to work. For example:</p>
  
  <pre><code>mostAppearing([5,5,2,2,1]); // correctly returns 5
  mostAppearing([3,4,1,6,10]); // correctly returns -1
  mostAppearing([4,7,7,7,9,9,8]); // correctly returns 7
  mostAppearing([4,7,7,9,7,9,9,8]); // correctly returns 9
  </code></pre>
  
  <p>And the function has the code:</p>
  
  <pre><code>function mostAppearing(arr) { // e.g. var arr = [4,7,7,9,7,9,9,8];
  
  var eachNumber = Array.from(new Set(arr)); // [4, 7, 9, 8];
  
  if (arr.length == eachNumber.length) {
      return -1;
  } else {
      var counts = eachNumber.map(function(c){ return arr.filter(function(el){ return el==c }).length }); // [1, 3, 3, 1];
      var maxVolume = Math.max(...counts); // 3
      var volVolume = counts.filter((c) =&gt; c == maxVolume).length; // 2
  
      if (volVolume == 1) {
          return arr[maxVolume];
      } else {
          var indexes = counts.reduce((a, c, i) =&gt; (c === maxVolume) ? a.concat(i) : a, []); // [1,2]
          var appearMost = [];
          indexes.map(function(c) { appearMost.push(eachNumber[c]) }); // relies on this line
          return Math.max(...appearMost);
      }
  }
  
  }
  </code></pre>
  
  <p>Can anyone explain (1) why undefined values are the result rather than [7,9], and (2) how my function works correctly? It should fail. Thanks for any help here.</p>