const create = () =>new Array(10).fill("_").map(() => parseInt(99 * Math.random()));const handler = (arr) => {let output = [],i = -1,edge;arr.sort((a, b) => a - b).forEach((item) => {let sItem = "" + item,newEdge;1 === sItem.length ? (newEdge = 9) : (newEdge = sItem[0] + "9");if (edge !== newEdge) {i++;edge = newEdge;output.push([]);}output[i].push(item);});return output;};console.log(handler(a));console.log(handler(create()));
