还有其他简单的方法可以更改我的原始 JavaScript 代码吗?

Is there any other simple way to change my vanilla JavaScript code?

有没有办法让我的代码更简单?

仅供参考,我是初学者,我只学习了 JavaScript 一周。

谢谢!

let array = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];
// from above to [[1, 1, 1, 1], [2, 2, 2], 4, 5, 10, [20, 20], 391, 392, 591]

const answer = (arr) => {
    arr.sort((a, b) => {
        return a - b;
    });
    let counter = 0;
    arr.forEach((num, i) => {
        if (arr[i] === arr[i+1]) {
            counter++;
        } else if (arr[i] !== arr[i + 1] && arr[i] === arr[i-1]) {
            arr[i-counter] = arr.slice(i-counter, i + 1);
            counter = 0;
        }
    });
    arr.forEach((num, i) => {
        while (arr[i][0] && arr[i][0] === arr[i + 1]) {
            arr.splice(i + 1, 1);
        }
    });
    return arr;
}

const newArray = answer(array);
console.log(newArray);

你可以简单点,先排序再分组。

let array = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20],
  last = null;

const result = array
  .sort((a, b) => a - b)
  .reduce((acc, curr, index, arr) => {
    // Getting the next element in array
    const next = arr[index + 1];
    
    if (last && last === curr) acc[acc.length - 1].push(curr);
    else if (next && next === curr) acc.push([curr]);
    else acc.push(curr);
     
    // Changing the last to the curr
    last = curr;
    
    return acc;
  }, []);

console.log(result);

使用 Map 对元素进行分组然后按键对 Map 条目进行排序以便完全不改变原始数组的方法

let array = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];

const answer = (arr) =>{
   const m = new Map();
   arr.forEach(n => m.set(n, (m.get(n) || []).concat(n)));   
   return [...m.entries()].sort(([a],[b])=> a-b)
                          .map(([k,v]) => v.length > 1 ? v : v[0])
}

console.log(answer(array))

另一种选择。首先将每个数字压入对象的适当数组,然后根据需要将对象转换为结果数组。

let arr = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];

const obj = {}, res = [];

for(e of arr) !obj[e] ? obj[e] = [e] : obj[e].push(e);
for(a in obj) obj[a].length === 1 ? res.push(obj[a][0]) : res.push(obj[a]);

console.log(res);