var a = [0, 1, 2, 0, 3, 4, 0, 1, 0, 0, 0] /** * 将数字0移动到数组末尾 * 在原数组操作 */ function moveZero(arr) { const arrLength = arr.length; if (arrLength < 1) return arr let j = -1; for (let i = 0; i < arrLength; i++) { const cur = arr[i] if (cur === 0 && j === -1) { j = i } // 0在左边 if (j < i && j >= 0 && arr[i] > 0) { let temp = arr[i] arr[i] = arr[j] arr[j] = temp j = i - 1 } } } moveZero(a) console.log(a)