Remove element from array, using slice

前端 未结 7 1426
野性不改
野性不改 2021-01-04 02:26

I am trying to remove a element from my array using slice, but i can\'t get it to work, look at this piece of code.

    console.log(this.activeEffects); // P         


        
7条回答
  •  离开以前
    2021-01-04 02:59

    Array.prototype.slice()...

    does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:

    Whereas Array.prototype.splice()...

    Changes the content of an array, adding new elements while removing old elements.

    This example should illustrate the difference.

    // sample array
    var list = ["a","b","c","d"];
    // slice returns a new array
    console.log("copied items: %o", list.slice(2));
    // but leaves list itself unchanged
    console.log("list: %o", list);
    // splice modifies the array and returns a list of the removed items
    console.log("removed items: %o", list.splice(2));
    // list has changed
    console.log("list: %o", list);

提交回复
热议问题