JavaScript Array splice vs slice

后端 未结 15 1277
刺人心
刺人心 2020-11-28 17:56

What is the difference between splice and slice?

$scope.participantForms.splice(index, 1);
$scope.participantForms.slice(index, 1);         


        
相关标签:
15条回答
  • 2020-11-28 18:18

    Here is a simple trick to remember the difference between slice vs splice

    var a=['j','u','r','g','e','n'];
    
    // array.slice(startIndex, endIndex)
    a.slice(2,3);
    // => ["r"]
    
    //array.splice(startIndex, deleteCount)
    a.splice(2,3);
    // => ["r","g","e"]
    

    Trick to remember:

    Think of "spl" (first 3 letters of splice) as short for "specifiy length", that the second argument should be a length not an index

    0 讨论(0)
  • 2020-11-28 18:20

    //splice
    var array=[1,2,3,4,5];
    console.log(array.splice(2));
    
    //slice
    var array2=[1,2,3,4,5]
    console.log(array2.slice(2));
    
    
    console.log("----after-----");
    console.log(array);
    console.log(array2);

    0 讨论(0)
  • 2020-11-28 18:25

    Splice and Slice both are Javascript Array functions.

    Splice vs Slice

    1. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.

    2. The splice() method changes the original array and slice() method doesn’t change the original array.

    3. The splice() method can take n number of arguments and slice() method takes 2 arguments.

    Splice with Example

    Argument 1: Index, Required. An integer that specifies at what position to add /remove items, Use negative values to specify the position from the end of the array.

    Argument 2: Optional. The number of items to be removed. If set to 0(zero), no items will be removed. And if not passed, all item(s) from provided index will be removed.

    Argument 3…n: Optional. The new item(s) to be added to the array.

    var array=[1,2,3,4,5];
    console.log(array.splice(2));
    // shows [3, 4, 5], returned removed item(s) as a new array object.
     
    console.log(array);
    // shows [1, 2], original array altered.
     
    var array2=[6,7,8,9,0];
    console.log(array2.splice(2,1));
    // shows [8]
     
    console.log(array2.splice(2,0));
    //shows [] , as no item(s) removed.
     
    console.log(array2);
    // shows [6,7,9,0]

    Slice with Example

    Argument 1: Required. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array.

    Argument 2: Optional. An integer that specifies where to end the selection but does not include. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.

    var array=[1,2,3,4,5]
    console.log(array.slice(2));
    // shows [3, 4, 5], returned selected element(s).
     
    console.log(array.slice(-2));
    // shows [4, 5], returned selected element(s).
    console.log(array);
    // shows [1, 2, 3, 4, 5], original array remains intact.
     
    var array2=[6,7,8,9,0];
    console.log(array2.slice(2,4));
    // shows [8, 9]
     
    console.log(array2.slice(-2,4));
    // shows [9]
     
    console.log(array2.slice(-3,-1));
    // shows [8, 9]
     
    console.log(array2);
    // shows [6, 7, 8, 9, 0]

    0 讨论(0)
  • 2020-11-28 18:25

    slice does not change original array it return new array but splice changes the original array.

    example: var arr = [1,2,3,4,5,6,7,8];
             arr.slice(1,3); // output [2,3] and original array remain same.
             arr.splice(1,3); // output [2,3,4] and original array changed to [1,5,6,7,8].
    

    splice method second argument is different from slice method. second argument in splice represent count of elements to remove and in slice it represent end index.

    arr.splice(-3,-1); // output [] second argument value should be greater then 
    0.
    arr.splice(-3,-1); // output [6,7] index in minus represent start from last.
    

    -1 represent last element so it start from -3 to -1. Above are major difference between splice and slice method.

    0 讨论(0)
  • S LICE = Gives part of array & NO splitting original array

    SP LICE = Gives part of array & SPlitting original array

    I personally found this easier to remember, as these 2 terms always confused me as beginner to web development.

    0 讨论(0)
  • 2020-11-28 18:27

    The splice() method returns the removed items in an array. The slice() method returns the selected element(s) in an array, as a new array object.

    The splice() method changes the original array and slice() method doesn’t change the original array.

    • Splice() method can take n number of arguments:

      Argument 1: Index, Required.

      Argument 2: Optional. The number of items to be removed. If set to 0(zero), no items will be removed. And if not passed, all item(s) from provided index will be removed.

      Argument 3..n: Optional. The new item(s) to be added to the array.

    • slice() method can take 2 arguments:

      Argument 1: Required. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array.

      Argument 2: Optional. An integer that specifies where to end the selection. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.

    0 讨论(0)
提交回复
热议问题