Find neighboring PINs on a numeric keypad
We can simplify this quite a bit.  At a high level, note that all you are doing is:


Converting each digit of the input to an array of possible neighbors.
Now you have an array of neighbor arrays.
The answer is simply the cartesian cross product of those neighbor arrays.
Sort them.
Turn the into back into strings.


The cartesian cross-product is a simple utility method.  

The neighbor possibilities are more compactly expressed as a JS object than by a switch statement, and this representation also makes it easy to map over them.

Putting it all together we get:

function xprod(...arrays) {
  return arrays.reduce((m, x) =&gt; m.flatMap(
    results =&gt; x.map(elm =&gt; results.concat([elm]))
  ), [ [] ])
}

function possibleCodes(code) {
  const neighbors = {
    '1': [1, 2, 4],
    '2': [1, 2, 3, 5],
    '3': [2, 3, 6],
    '4': [1, 4, 5, 7],
    '5': [2, 4, 5, 6, 8],
    '6': [3, 5, 6, 9],
    '7': [4, 7, 8],
    '8': [0, 5, 7, 8, 9],
    '9': [6, 8, 9],
    '0': [0, 8],
  }
  const possibilitiesByDigit = code.split('').map(x =&gt; neighbors[x])
  return xprod(...possibilitiesByDigit).map(x =&gt; x.join('')).sort()
}