编辑代码

const nums = [1,2,3,0,4,8,6,2,0,7,0]
function moveZeroToEnd(nums){
    const newArr = nums.filter(item => item !== 0);
    return [...newArr, ...Array(nums.length - newArr.length).fill(0)]
}
console.log(moveZeroToEnd(nums))
function moveZeroToEnd1(nums){
    let preIndex = 0, nowIndex = 0;
    const len = nums.length;
    for(;preIndex < len; preIndex++) {
        if(nums[preIndex] !== 0 ) {
            nums[nowIndex++] = nums[preIndex]
        }
    }
    for(;nowIndex < len; nowIndex++) {
        nums[nowIndex] = 0
    }
}
console.log(moveZeroToEnd1(nums))