sisterAn / JavaScript-Algorithms

基础理论+JS框架应用+实践,从0到1构建整个前端算法体系
5.45k stars 626 forks source link

1、大佬这里的 over 需要 `over > 0` 吧? #167

Closed xllpiupiu closed 2 years ago

xllpiupiu commented 2 years ago

1、大佬这里的 over 需要 over > 0 吧?

    let over = addCount - delCount;
    if(over) {
        // 向后移

2、这块的逻辑处理好像不太对(稀疏数组也存在类似情况)。

} else if (over < 0) {
        // 向前移
        for(let i = startIndex + delCount; i <= array.length - 1; i++) {
            if(i + Math.abs(over) > array.length - 1) {
                // 删除冗于元素
                delete array[i]
                continue
            }
            array[i] = array[i + Math.abs(over)]
        }
    }
}

例如:

var arr = [1,2,3,4,5];
arr.splice(0, 3, 6, 7);

console.log(arr); // 
- 预期结果:[6, 7, 4, 5]
- 实际结果:[6, 7, 3, 5]

Originally posted by @MeetTheBest in https://github.com/sisterAn/JavaScript-Algorithms/issues/138#issuecomment-846373010