将相同的颜色放置在一起<物以类聚>
将相同的颜色放置在一起
function colorTogether(arr) { let newArr = []; for (let index = 0; index < arr.length; index++) { let indexarr = newArr.indexOf(arr[index]); if(indexarr==-1){ newArr.push(arr[index]) }else { newArr.splice(indexarr,0,arr[index]) } } return newArr } let arrColor = [1,1,0,2,0,1,2,0,1,0,2]; console.log(colorTogether(arrColor));