GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 第 158 题:如何模拟实现 Array.prototype.splice · Issue #384 · Advanced-Frontend/Daily-Interview-Question

第 158 题:如何模拟实现 Array.prototype.splice #384

New issue
Copy link
New issue
Copy link
Open
Open
第 158 题:如何模拟实现 Array.prototype.splice#384
Copy link
Labels
字节编程题

Description

@yygmind
yygmind
opened on May 19, 2020
Issue body actions
No description provided.

Activity

Next
  1. yygmind
    added
    字节
    编程题
    on May 19, 2020
  2. kt3721 commented on May 19, 2020

    @kt3721
    kt3721
    on May 19, 2020
    More actions
    Array.prototype._splice = function (start, deleteCount, ...addList) {
            if (start < 0) {
                if (Math.abs(start) > this.length) {
                    start = 0
                } else {
                    start += this.length
                }
            }
    
            if (typeof deleteCount === 'undefined') {
                deleteCount = this.length - start
            }
    
            const removeList =  this.slice(start, start + deleteCount)
    
            const right = this.slice(start + deleteCount)
    
            let addIndex = start
            addList.concat(right).forEach(item => {
                this[addIndex] = item
                addIndex++
            })
            this.length = addIndex
    
            return removeList
        }
    Reacted by wangh1998, lkanzero, freecici, zengpeng123, Charles Duan, Coder, wenchaoding, xlf, Alan.He, fanchong02 and 13 more
    Reacted by liuwenjun, tushuang and Velove
  3. hexuan-aurora commented on May 19, 2020

    @hexuan-aurora
    hexuan-aurora
    on May 19, 2020
    More actions
    Array.prototype.splice = function(start, deleteCount, ...items) {
        let deletedItems = [];
    
        if (Object.is(arguments.length, 0)) return deletedItems;
    
        const length = this.length;
    
        let actualStart = +start || 0;
        actualStart =  actualStart >= 0 ? Math.min(actualStart, length) : Math.max(length + actualStart, 0); // 防止 start 越界
    
        let actualDeleteCount;
        if (Object.is(arguments.length, 1)) {
            actualDeleteCount = length - actualStart; // 未传 deleteCount 时默认删除 start 右侧所有元素
        } else {
            actualDeleteCount = Math.min(Math.max(+deleteCount, 0), length - actualStart); // 防止 deleteCount 大于 start 右侧所有元素长度
        }
    
        deletedItems = this.slice(actualStart, actualStart + actualDeleteCount); // 待删除元素
    
        const resetItems = this.slice(actualStart + actualDeleteCount); // 右侧剩余元素
        [...items, ...resetItems].forEach(item => this[actualStart++]=item);  // 将待添加元素与右侧剩余元素添加至原数组中
        
        this.length = actualStart;
    
        return deletedItems;
    }
    
    Reacted by FangYu and xlf
  4. zitup commented on May 19, 2020

    @zitup
    zitup
    on May 19, 2020 · Hidden as off-topic
    show commentMore actions
  5. yaoqifa commented on May 19, 2020

    @yaoqifa
    yaoqifa
    on May 19, 2020
    More actions

    Array.prototype.mySplice = function (start, count, ...args) {
    const len = this.length
    let _start = start
    let res = []
    let thisArr = []
    function setThis(that, arr) {
    arr.forEach((v, index) =>that[index] = v)
    that.length = arr.length
    }
    if (start < 0) {
    _start = len + start
    _start = _start < 0 ? 0 : _start
    }
    if (start > len) {
    _start = len
    }
    if (count === undefined) {
    res = this.slice(_start)
    setThis(this, this.slice(0, _start))
    return res
    }
    res = this.slice(_start, _start + count)
    const left = this.slice(0, _start)
    const right = this.slice(_start + count)
    setThis(this, left.concat(args, right))
    return res
    }

  6. weiweixuan commented on May 21, 2020

    @weiweixuan
    weiweixuan
    on May 21, 2020
    More actions
    // splice
          let arr1 = [1, 3, 5, 7, 9];
          let arr2 = [1, 3, 5, 7, 9];
          /*
              一个参数表示删除后面所有元素
              两个参数表示走a开始删b个元素
              两个以上表示走a开始删b个元素,再这个位置添加元素
            */
          Array.prototype.mySplice = function (index, delCount, ...args) {
            // 判断this
            if (this === null || this === undefined) {
              throw new TypeError(
                "Cannot read property 'map' of null or undefined"
              );
            }
            let O = Object(this),
              len = O.length;
            // 第一个参数,不存在 (返回空数组)
            if (index === undefined) {
              return [];
            }
            index = Number(index);
            // 第一个参数不是数字  (全删除)
            if (isNaN(index)) {
              return delArray(O, 0);
            }
            // index若为负值或者超过length
            index = changeIndex(index, len);
    
            // 第二个参数没有 (后面的全删除)
            if (delCount === undefined) {
              return delArray(O, index);
            }
            // 第二个参数不是数字 (返回空数组)
            if (isNaN(delCount)) {
              return [];
            } else {
              // 第三个参数存在
              if (Array.isArray(args)) {
                return add(O, index, delCount, args);
              }
              // 不存在
              // 删除数组
              return delArray(O, index, delCount);
            }
          };
          function changeIndex(index, len) {
            // 若为负值
            while (index < 0) {
              index += len;
            }
            // 若为超过len的值
            if (index >= len) {
              index = len;
            }
            return index;
          }
          // 添加元素
          function add(arr, index, count, temp) {
            // 先删除
            let a = delArray(arr, index, count);
            // 添加元素
            for (let i = 0; i < temp.length; i++) {
              // 外层循环添加次数
              for (let j = arr.length; j > index + i; j--) {
                arr[j] = arr[j - 1];
              }
              arr[index + i] = temp[i];
            }
            return a;
          }
          // 删除元素
          function delArray(arr, index, count = arr.length) {
            let temp = [],
              len = arr.length;
            //判断有效的count
            count = len - index >= count ? count : len - index;
            for (let i = 0; i < count; i++) {
              let item = arr[index + i];
              temp.push(item);
            }
            while (count) {
              count--;
              for (let j = index; j < arr.length - 1; j++) {
                arr[j] = arr[j + 1];
              }
              let len_ = arr.length - 1;
              delete arr[len_];
              arr.length = len_;
            }
            return temp;
          }
          let res1 = arr1.mySplice(1, 1, 111, 222, 333);
          let res2 = arr2.splice(1, 1, 111, 222, 333);
          console.log(res1, arr1, res2, arr2);
    Reacted by AntHunt2 and mengzuile
    Reacted by Mr.V and Jontyy
  7. wulichenyang commented on May 22, 2020

    @wulichenyang
    wulichenyang
    on May 22, 2020
    More actions

    前端的原始API还有没写的吗?

    Reacted by lujiayu666666 and cescxu0712
    Reacted by neighbor wang, hugeorange, cescxu0712, culeung and Bobobo
  8. listentolife commented on May 24, 2020

    @listentolife
    listentolife
    on May 24, 2020
    More actions
    Array.prototype.splice = function (start, deleteCount, ...args) {
      // 处理start值
      if (start < 0) {
        if (Math.abs(start) > this.length - 1) {
          start = 0
        } else {
          start += this.length
        }
      }
      // 处理deleteCount值
      deleteCount = typeof deleteCount !== 'undefined' ? deleteCount : this.length
      // 处理args值
      args = args.length ? args : []
      // 处理特殊情况
      if (start > this.length - 1) {
        this.concat(args)
        return []
      }
      /**
       * 思路
       * 先把原数组值取出,原数组清空
       * 先入栈不需要处理的数组前部分元素
       * 然后把需要加入的args
       * 最后入栈数组后部分元素
       * 返回中间删掉的元素组成的数组
       */
      let arr = [...this]
      this.length = 0 // 清空
      // 先入栈前部分元素
      let i = 0
      while (i < start) {
        this.push(arr.shift())
        i++
      }
      // 入栈args
      args.forEach(item => this.push(item))
      // 入栈后部分元素
      arr.forEach((item, index) => {
        if (index >= deleteCount) {
          this.push(item)
          delete arr[index]
        }
      })
      // 返回删除部分
      return arr
    }
    Reacted by Dylan
  9. 1eeing commented on May 26, 2020

    @1eeing
    1eeing
    on May 26, 2020
    More actions
    //@ts-ignore
    Array.prototype._splice = function (index: number, count: number, ...args: any[]) {
      const deletes = [];
      let res = [];
      let pushIndex;
      for(let i = 0; i < this.length; i++){
          if(i === index && args.length){
              pushIndex = i;
          }
          if(i >= index && i < (index+count)){
              deletes.push(this[i]);
              continue;
          }
          res.push(this[i]);
      }
      if(pushIndex !== void 0){
          const left = res.slice(0, pushIndex);
          const right = res.slice(pushIndex, res.length);
          res = [...left, ...args, ...right];
      }
      this.length = 0;
      this.push(...res);
      return deletes;
    }
  10. sheepshine commented on May 26, 2020

    @sheepshine
    sheepshine
    on May 26, 2020
    More actions
    Array.prototype.spliceMy = function (startIndex, endIndex, ...item) {
        if (startIndex < 0) {
            startIndex = this.length + startIndex
            endIndex = this.length + startIndex + endIndex
        } else {
            endIndex = startIndex + endIndex
        }
        let arrStart = this.slice(0, startIndex)
        let arrEnd = this.slice(endIndex, this.length)
        console.log(arrStart, arrEnd)
        let returnArr = this.slice(startIndex, endIndex)
        let newArr = arrStart.concat(item).concat(arrEnd)
        for (let i = 0; i < newArr.length; i++) {
            this[i] = newArr[i];
        }
        this.length = newArr.length
        return returnArr
    }
  11. fengqinglingyu commented on Jun 1, 2020

    @fengqinglingyu
    fengqinglingyu
    on Jun 1, 2020
    More actions
    /**
     * @param {number} idx 开始删除的索引
     * @param {number} deleteCount 需要删除的数目
     * @param {any[]} insertItems 需要插入项
     * @returns {any[]} 被删的数组项的数组
     */
    Array.prototype.mySplice = function (idx, deleteCount) {
      var arr = this;
      var length = arr.length;
      var insertItems = [].slice.call(arguments, 2);
      deleteCount =
        typeof deleteCount !== 'undefined' && deleteCount <= length - idx
          ? deleteCount
          : length - idx;
      var res = [];
      while (deleteCount) {
        deleteCount--;
        if (idx in arr) {
          res.push(arr[idx]);
        }
        for (var i = idx; i < arr.length - 1; i++) {
          arr[i] = arr[i + 1];
        }
        arr.length = arr.length - 1;
      }
      for (var j = 0; j < insertItems.length; j++) {
        arr.push(insertItems[j]);
      }
      return res;
    };

    写了一个TS的,编译了下

    Reacted by Pandas
  12. ZangYuSong commented on Jun 8, 2020

    @ZangYuSong
    ZangYuSong
    on Jun 8, 2020
    More actions

    有几点需要注意的:

    1、第一个参数,开始下标
    2、第二个参数,删除个数
    3、从第三个开始往后都是插入的数据

    4、操作的都是原数组,也就是改变的是原数组
    5、返回一个被删除的数据的新数组

    下边这个是我老早之前写的,参考了 chrome 的实现方式

    Array.prototype._splice = function () {
      if (arguments.length === 0) return [];
      var arg_len = arguments.length,
        array_len = this.length,
        index = parseInt(arguments[0]) ? parseInt(arguments[0]) : 0,
        num = parseInt(arguments[1])
          ? parseInt(arguments[1]) < 0
            ? 0
            : parseInt(arguments[1])
          : 0;
      // 起始下标
      if (index < 0) {
        index += array_len;
        if (index < 0) {
          index = 0;
        }
      }
      // 删除个数
      if (index + num > array_len) {
        num = array_len - index;
      }
      // 删除
      var new_array = [];
      if (num > 0) {
        for (var i = 0, k = 0; i < array_len; i++) {
          if (i !== index) {
            this[k++] = this[i];
          } else {
            for (var j = 0; j < num; j++) {
              new_array[j] = this[i++];
            }
            i--;
          }
        }
      }
      // 插入数据
      this.length = array_len - num + (arg_len < 2 ? 0 : arg_len - 2);
      if (arg_len === 2) {
        return new_array;
      }
      array_len -= num;
      for (i = this.length - 1; i >= 0; i--) {
        if (i !== index + arg_len - 3) {
          this[i] = this[--array_len];
        } else {
          for (j = arg_len - 1; j > 1; j--, i--) {
            this[i] = arguments[j];
          }
          i++;
        }
      }
      return new_array;
    };
  13. czz362100 commented on Jun 9, 2020

    @czz362100
    czz362100
    on Jun 9, 2020
    More actions

    /***

    • index: 删除的起始下标
    • num:删除个数
    • addlist: 新增的元素
    • returnItem :返回已删除元素
      */
    Array.prototype._slice = function(index, num, ...addList) {
        let newArr = []
        let returnItem = []
        while(num > 0) {
            // 起始下标有可能传入负数
            if (index < 0) {
                index += this.length
            }
            // 删除当前项
            returnItem.push(this[index])
            delete this[index]
            index++
            num--
        }
        // 将老数组赋予新数组
        for(const item of arr) {
            if(item) newArr.push(item)
        }
        let length = newArr.length
        for(var i = 0; i< length;i++) {
            this[i] = newArr[i]
        }
        this.length = length
        // 新增元素
        if(addList && addList.length > 0) {
            for(const item of addList) {
                this.push(item)
            }
        }
        return returnItem
    }
    
    Reacted by 小泽老师
  14. GuoYiJi commented on Jun 12, 2020

    @GuoYiJi
    GuoYiJi
    on Jun 12, 2020
    More actions
    (function () {
    
      const del = (arr, index, len) => {
        if (index < 0) {
          index = arr.length - Math.abs(index);
        }
        let i = index + len;
        let result = [];
    
    
        let count = len;
        let offset = index;
        while (count--) {
          result.push(arr[offset++]);
        }
    
        while (i < arr.length) {
          arr[i - len] = arr[i];
          i++;
        }
    
        arr.length = arr.length - len;
        return result;
    
      }
    
      const insert = (arr, index, items) => {
        // arr.length = arr.length + items.length;
    
        let i = arr.length - 1;
    
        while (i >= index) {
          arr[i + items.length] = arr[i];
          i--;
        }
        i = 0;
        while (i < items.length) {
          arr[i + index] = items[i];
          i++
        }
    
      }
      
      Array.prototype._splice = function _splice(index, deleteCount, ...rest) {
    
        let arr = this;
        let removed = [];
      
        // 只有一个元素
        if (deleteCount === undefined) {
          removed = del(arr, index, arr.length - Math.abs(index));
        } else {
          removed = del(arr, index, deleteCount);
          if (rest.length) {
            insert(arr, index, rest);
          }
        }
      
      
        return removed;
      }
      var myFish = ["angel", "clown", "mandarin", "sturgeon"];
      var removed = myFish._splice(2, 0, "drum");
      console.log(myFish, removed)
      
      // 运算后的 myFish: ["angel", "clown", "drum", "mandarin", "sturgeon"]
      // 被删除的元素: [], 没有元素被删除
      
      var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
      var removed = myFish._splice(2, 0, 'drum', 'guitar');
      console.log(myFish, removed)
      
      // 运算后的 myFish: ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
      // 被删除的元素: [], 没有元素被删除
      
      var myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon'];
      var removed = myFish._splice(3, 1);
      console.log(myFish, removed)
      
      // 运算后的 myFish: ["angel", "clown", "drum", "sturgeon"]
      // 被删除的元素: ["mandarin"]
    
      var myFish = ['angel', 'clown', 'drum', 'sturgeon'];
      var removed = myFish._splice(2, 1, "trumpet");
      console.log(myFish, removed)
    
      // 运算后的 myFish: ["angel", "clown", "trumpet", "sturgeon"]
      // 被删除的元素: ["drum"]
    
      var myFish = ['angel', 'clown', 'trumpet', 'sturgeon'];
      var removed = myFish._splice(0, 2, 'parrot', 'anemone', 'blue');
      console.log(myFish, removed)
    
      // 运算后的 myFish: ["parrot", "anemone", "blue", "trumpet", "sturgeon"]
      // 被删除的元素: ["angel", "clown"]
    
      var myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon'];
      var removed = myFish._splice(myFish.length - 3, 2);
      console.log(myFish, removed)
    
      // 运算后的 myFish: ["parrot", "anemone", "sturgeon"]
      // 被删除的元素: ["blue", "trumpet"]
    
      var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
      var removed = myFish._splice(-2, 1);
      console.log(myFish, removed)
    
      // 运算后的 myFish: ["angel", "clown", "sturgeon"]
      // 被删除的元素: ["mandarin"]
    
      var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
      var removed = myFish._splice(2);
      console.log(myFish, removed)
    
      // 运算后的 myFish: ["angel", "clown"]
      // 被删除的元素: ["mandarin", "sturgeon"]
    
    })()
  15. 17 remaining items

  16. zhangfangbiao commented on Mar 3, 2021

    @zhangfangbiao
    zhangfangbiao
    on Mar 3, 2021
    More actions
    Array.prototype.splice2 = function(start, count, ...addList) {
        if (start < 0) {
            if (Math.abs(start) > this.length) {
                start = 0
            } else {
                start = this.length + start
            }
        }
        if (typeof count !== 'number') {
            count = this.length - start
        } else {
            if (count < 0) {
                count = 0
            }
        }
        let delList = this.slice(start, start + count)
        let right = this.slice(start + count, this.length)
        this.length = start
        this.push(...(addList.concat(right)))
        return delList
    }
    Reacted by haipingzi
  17. qzruncode commented on Apr 13, 2021

    @qzruncode
    qzruncode
    on Apr 13, 2021
    More actions
    Array.prototype.spliceLike = function(start, num, ...params) {
        return [...this.slice(0, start), ...params, ...this.slice(start + num)]
    }
    Reacted by 叶家伟
  18. haipingzi commented on Apr 26, 2021

    @haipingzi
    haipingzi
    on Apr 26, 2021
    More actions
        Array.prototype._splice = function (start, count, ...replaceEl) {
          if (typeof start !== "number") throw new Error("start not a number");
          if (typeof count !== "number") throw new Error("count not a number");
          start = start < 0 ? Math.max(start + this.length, 0) : start; // 开始值为负数要加上数组长度且不能小于零;
          Math.max(0, count); // 删除数量大于等于0;
          const delArr = this.slice(start, start + count);
          const resArr = [
            ...this.slice(0, start),
            ...replaceEl,
            ...this.slice(start + count),
          ];
          delArr.forEach((item, index) => {
            delete this[index];
          });
          resArr.forEach((item, index) => {
            this[index] = item;
          });
         this.length = resArr.length;
          return delArr;
        };
  19. haipingzi commented on Apr 26, 2021

    @haipingzi
    haipingzi
    on Apr 26, 2021
    More actions
    Array.prototype.splice2 = function(start, count, ...addList) {
        if (start < 0) {
            if (Math.abs(start) > this.length) {
                start = 0
            } else {
                start = this.length + start
            }
        }
        if (typeof count !== 'number') {
            count = this.length - start
        } else {
            if (count < 0) {
                count = 0
            }
        }
        let delList = this.slice(start, start + count)
        let right = this.slice(start + count, this.length)
        this.length = start
        this.push(...(addList.concat(right)))
        return delList
    }

    const arr1 = [1, 2, 3, 4, 5];
    const arr2 = [1, 2, 3, 4, 5];
    arr1.splice2(100, 1, [1, 2])
    arr2.splice(100, 1, [1, 2])
    console.log(arr1)
    console.log(arr2)
    //大于数组长度就不正确了

  20. haipingzi commented on Apr 26, 2021

    @haipingzi
    haipingzi
    on Apr 26, 2021
    More actions
    Array.prototype._splice = function (start, deleteCount, ...addList) {
            if (start < 0) {
                if (Math.abs(start) > this.length) {
                    start = 0
                } else {
                    start += this.length
                }
            }
    
            if (typeof deleteCount === 'undefined') {
                deleteCount = this.length - start
            }
    
            const removeList =  this.slice(start, start + deleteCount)
    
            const right = this.slice(start + deleteCount)
    
            let addIndex = start
            addList.concat(right).forEach(item => {
                this[addIndex] = item
                addIndex++
            })
            this.length = addIndex
    
            return removeList
        }

    sart 大于数组长度就不对了
    const arr1 = [1, 2, 3, 4, 5];
    const arr2 = [1, 2, 3, 4, 5];
    arr1._splice(100, 1, [1, 2])
    arr2.splice(100, 1, [1, 2])
    console.log(arr1) //  [1, 2, 3, 4, 5, empty × 95, Array(2)]
    console.log(arr2) // [1, 2, 3, 4, 5, Array(2)]

    Array.prototype._splice = function (start, deleteCount, ...addList) {
            if (start < 0) {
                if (Math.abs(start) > this.length) {
                    start = 0
                } else {
                    start += this.length
                }
            }
    
            if (typeof deleteCount === 'undefined') {
                deleteCount = this.length - start
            }
    
            const removeList =  this.slice(start, start + deleteCount)
    
            const right = this.slice(start + deleteCount)
    
            let addIndex = start
            addList.concat(right).forEach(item => {
                this[addIndex] = item
                addIndex++
            })
            this.length = addIndex
    
            return removeList
        }

    start大于数组长度就不对了

  21. z1948296027 commented on May 15, 2021

    @z1948296027
    z1948296027
    on May 15, 2021
    More actions
    Array.prototype.splice = function(index, num, ...rest) {
        let context = this
        let removeList = []
        //index为负值
        if (index < 0) {
            index = index + context.length
        }
        //num存在
        if(num != null) {
            if (num !== 0) {
                //查找要删除的元素
                for (let i = index; i < index + num; i++) {
                    removeList.push(context[i])
                }
                //移除要移除的元素
                const conLength = context.length
                for (let i = index + num; i < conLength; i++) {
                    context[i - num] = context[i]
                }
                context.length = conLength - num
            }
            //添加要添加的元素
            if (rest.length > 0) {
                const restLength = rest.length
                const conLength = context.length
                for(let i = conLength - 1; i >= index; i--) {
                    context[i+restLength] = context[i]
                }
                
                let count = 0
                for(let i = index; i < index+restLength; i++) {
                    context[i] = rest[count]
                    count++
                }
            }
        } else {
            //查找要删除的元素
            const conLength = context.length
            for (let i = index; i < conLength; i++) {
                removeList.push(context[i])
            }
            context.length = index
        }
        return removeList
    }
    var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
    var removed = myFish.splice(2, 0, 'drum', 'guitar');
    
    console.log(myFish)
    console.log(removed)
    //应该把数组第index位左移或右移n位抽象成函数,看起来应该就清楚多了
  22. poppydani commented on Jul 9, 2021

    @poppydani
    poppydani
    on Jul 9, 2021
    More actions
    Array.prototype._splice = function (start, delCount, ...addItems) {
      const arr = this;
      const res = [];
      const left = arr.slice(0, start);
      const right = arr.slice(start + delCount)
      const remove = arr.slice(start, start + delCount); // 被删除的元素
    
      arr.length = left.length;
      addItems.forEach(item => arr.push(item));
      right.forEach(item => arr.push(item));
      return remove;
    }
  23. Jobs-Chen commented on May 12, 2022

    @Jobs-Chen
    Jobs-Chen
    on May 12, 2022
    More actions
    Array.prototype._splice = function (start, deleteCount, ...addList) {
            if (start < 0) {
                if (Math.abs(start) > this.length) {
                    start = 0
                } else {
                    start += this.length
                }
            }
    
            if (typeof deleteCount === 'undefined') {
                deleteCount = this.length - start
            }
    
            const removeList =  this.slice(start, start + deleteCount)
    
            const right = this.slice(start + deleteCount)
    
            let addIndex = start
            addList.concat(right).forEach(item => {
                this[addIndex] = item
                addIndex++
            })
            this.length = addIndex
    
            return removeList
        }

    你这个有问题,当start不小于0却大于数组长度的时候你这个会错误的导致数组长度变大

  24. listentolife commented on May 12, 2022

    @listentolife
    listentolife
    on May 12, 2022 via email
    More actions
    你的邮件我已收到,我会马上查看! —————————————————————— 现在的追逐梦想是为了日后能拥有美好的“那些年”!
  25. Jobs-Chen commented on May 12, 2022

    @Jobs-Chen
    Jobs-Chen
    on May 12, 2022
    More actions
    Array.prototype._splice = function(start, delCount, ...addList) {
        // 当起始位置超出了数组的长度,则从数组末尾开始添加内容
        if(start > this.length) {
            start = this.length;
            delCount = 0;
        }
        if(start < 0) {
            // 超出数组长度的话设置为0
            if(Math.abs(start) > this.length) {
                start = 0;
            } else {
                // 如果起始值为负数,则起始位置为数组末尾开始第几位
                start += this.length;
            }
        }
        // 设置删除的长度,没有值的话相当于移除起始位置之后的所有元素
        if(delCount === undefined) {
            delCount = this.length - start;
        }
        // 删除的元素
        const removeList = this.slice(start, start + delCount);
        // 删除元素的右侧数据
        const right = this.slice(start + delCount);
    
        // 将新加入的数据和右侧剩余的数据合并,然后将数组下标对应位置的元素替换
        let addStart = start;
        addList.concat(right).forEach(item => {
            this[addStart] = item;
            addStart++;
        });
        // 修改数组长度
        this.length = addStart;
        // 返回删除的元素
        return removeList;
    }

    这里附上MDN对于这个方法的定义Array.prototype.splice()
    参数
    start​
    指定修改的开始位置(从0计数)。如果超出了数组的长度,则从数组末尾开始添加内容;如果是负值,则表示从数组末位开始的第几位(从-1计数,这意味着-n是倒数第n个元素并且等价于array.length-n);如果负数的绝对值大于数组的长度,则表示开始位置为第0位。
    deleteCount 可选
    整数,表示要移除的数组元素的个数。如果 deleteCount 大于 start 之后的元素的总数,则从 start 后面的元素都将被删除(含第 start 位)。如果 deleteCount 被省略了,或者它的值大于等于array.length - start(也就是说,如果它大于或者等于start之后的所有元素的数量),那么start之后数组的所有元素都会被删除。如果 deleteCount 是 0 或者负数,则不移除元素。这种情况下,至少应添加一个新元素。
    item1, item2, ... 可选
    要添加进数组的元素,从start 位置开始。如果不指定,则 splice() 将只删除数组元素。
    返回值
    由被删除的元素组成的一个数组。如果只删除了一个元素,则返回只包含一个元素的数组。如果没有删除元素,则返回空数组。

  26. lucaslmlok commented on Jun 20, 2022

    @lucaslmlok
    lucaslmlok
    on Jun 20, 2022
    More actions
    Array.prototype._splice = function (start, deleteCount, ...items) {
      if (start < 0) {
        start += this.length;
      }
      start = Math.max(start, 0);
    
      if (deleteCount === 'undefined') {
        deleteCount = this.length - start;
      }
    
      if (start >= this.length) {
        deleteCount = 0;
      }
      deleteCount = Math.max(deleteCount, 0);
    
      const length = this.length;
      const right = [];
      const deleted = [];
    
      for (let i = start + deleteCount; i < length; i++) {
        right.unshift(this.pop());
      }
    
      for (let i = start; i < start + deleteCount; i++) {
        deleted.unshift(this.pop());
      }
    
      for (let i = 0; i < items.length; i++) {
        this.push(items[i]);
      }
    
      for (let i = 0; i < right.length; i++) {
        this.push(right[i]);
      }
    
      return deleted;
    };
  27. Yangfan2016 commented on Aug 18, 2022

    @Yangfan2016
    Yangfan2016
    on Aug 18, 2022
    More actions

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

    start
    如果是负数,判断abs 值如果大于 length ,则 start置为 0 否则 start取 length-start
    否则(非负数),直接取 start

    deleteCount
    要删除的个数
    如果不指定,或 deleteCount>=length 则将 start 后的 数都删除
    如果取值为0或负数,则不进行删除操作
    否则删除 包含 start 及之后的 共 deleteCount 个数

    item1,item2,...

    要插入的元素

  28. listentolife commented on Aug 18, 2022

    @listentolife
    listentolife
    on Aug 18, 2022 via email
    More actions
    你的邮件我已收到,我会马上查看! —————————————————————— 现在的追逐梦想是为了日后能拥有美好的“那些年”!
  29. xiaoguoaa commented on Mar 16, 2023

    @xiaoguoaa
    xiaoguoaa
    on Mar 16, 2023
    More actions
    Array.prototype._splice = function(index, deleteCount, ...items) {
      if (index < 0) {
        index = this.length + index;
      }
      if (deleteCount === undefined) {
        deleteCount = this.length - index;
      }
      if (deleteCount < 0) {
        deleteCount = 0;
      }
      let res = this.slice(index, index + deleteCount);
      let temp = [...this.slice(0, index), ...items, ...this.slice(index + deleteCount)];
      if (temp.length < this.length) {
        this.length = temp.length;
      }
      for (let i = 0; i < temp.length; i++) {
        this[i] = temp[i];
      }
      return res;
    };
  30. ZegTsai commented on Aug 29, 2023

    @ZegTsai
    ZegTsai
    on Aug 29, 2023
    More actions
    function mySplice(start, deleteNum, ...newItem) {
      // 传参检查与转换
      if (typeof start !== "number" || deleteNum < 0) return [];
      start < 0 && (start = Math.abs(this.length + start));
      typeof deleteNum !== "number" && (deleteNum = this.length - start);
    
      const removeList = this.slice(start, start + deleteNum),
        newList = [
          ...this.slice(0, start),
          ...newItem,
          ...this.slice(start + deleteNum),
        ];
      this.length += newItem.length - deleteNum;
      let p = 0;
      while (p < this.length) {
        this[p] = newList[p++];
      }
      return removeList;
    }
    
    Array.prototype.mySplice = mySplice;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    字节编程题

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.