Title: generating a random integer type array of length 10 of, for example, [2, 10, 3, 4, 5, 11, 10, 11, 20 ], which are arranged in the following a new array, a new array of requirements, e.g. [[2, 3, 4, 5], [10, 11], [20]].
answer:
function formArray (ARR) { const sortedArr = Array.from ( new new the Set (ARR)) Sort ((A, B) => A -. B); // define a Map const = Map new new the Map (); sortedArr.forEach ((V) => { // get the index const Math.floor Key = (V / 10 ); // get into map the subscript value const Group = as map.get (Key) || []; / / Push current value group.push (V); // refresh target current value map map.set (key, group); }); // deconstruction returned array return [... map.values ()]; } // find the continuous version function formArray1 (ARR) { const sortedArr = Array.from ( new new the Set (ARR)) Sort ((. a, B) => a - B); return sortedArr.reduce ((ACC, CUR) => { // get the current array and finally a last array value const lastArr = acc.slice () pop ( ). || []; const lastval = lastArr.slice () POP ();. // lastval = null this judgment of undetermined significance, intuitively, even if the first cycle is not null, but undefined! IF (lastval! = nullCUR && - === lastval. 1 ) { // throw a last array in lastArr.push (CUR); } the else { // the last memory array becomes an up acc.push ([cur]); } return ACC; }, []); } function genNumArray (NUM, Base = 100 ) { return Array.from ({length: NUM}, () => Math.floor (Math.random () * Base)); } const ARR = genNumArray (10, 20 is); // [2, 10,. 3,. 4,. 5,. 11, 10,. 11, 20 is]; const = RES formArray (ARR); const res1 = formArray1(arr); console.log(`res ${JSON.stringify(res)}`);