JavaScript Array splice vs slice

后端 未结 15 1275
刺人心
刺人心 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:11

    JavaScript Array splice() Method By Example

    Example1 by tutsmake -Remove 2 elements from index 1

      var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; 
    
     arr.splice(1,2);
    
     console.log( arr ); 

    Example-2 By tutsmake – Add new element from index 0 JavaScript

      var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; 
    
     arr.splice(0,0,"zero");
    
     console.log( arr );  

    Example-3 by tutsmake – Add and Remove Elements in Array JavaScript

    var months = ['Jan', 'March', 'April', 'June'];
    
    months.splice(1, 0, 'Feb'); // add at index 1
    
    console.log(months); 
    
    months.splice(4, 1, 'May'); // replaces 1 element at index 4
    
    console.log(months);

    https://www.tutsmake.com/javascript-array-splice-method-by-example/

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

    Splice - MDN reference - ECMA-262 spec

    Syntax
    array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

    Parameters

    • start: required. Initial index.
      If start is negative it is treated as "Math.max((array.length + start), 0)" as per spec (example provided below) effectively from the end of array.
    • deleteCount: optional. Number of elements to be removed (all from start if not provided).
    • item1, item2, ...: optional. Elements to be added to the array from start index.

    Returns: An array with deleted elements (empty array if none removed)

    Mutate original array: Yes

    Examples:

    const array = [1,2,3,4,5];
    
    // Remove first element
    console.log('Elements deleted:', array.splice(0, 1), 'mutated array:', array);
    // Elements deleted: [ 1 ] mutated array: [ 2, 3, 4, 5 ]
    
    // array = [ 2, 3, 4, 5]
    // Remove last element (start -> array.length+start = 3)
    console.log('Elements deleted:', array.splice(-1, 1), 'mutated array:', array);
    // Elements deleted: [ 5 ] mutated array: [ 2, 3, 4 ]

    More examples in MDN Splice examples


    Slice - MDN reference - ECMA-262 spec

    Syntax
    array.slice([begin[, end]])
    Parameters

    • begin: optional. Initial index (default 0).
      If begin is negative it is treated as "Math.max((array.length + begin), 0)" as per spec (example provided below) effectively from the end of array.
    • end: optional. Last index for extraction but not including (default array.length). If end is negative it is treated as "Math.max((array.length + begin),0)" as per spec (example provided below) effectively from the end of array.

    Returns: An array containing the extracted elements.

    Mutate original: No

    Examples:

    const array = [1,2,3,4,5];
    
    // Extract first element
    console.log('Elements extracted:', array.slice(0, 1), 'array:', array);
    // Elements extracted: [ 1 ] array: [ 1, 2, 3, 4, 5 ]
    
    // Extract last element (start -> array.length+start = 4)
    console.log('Elements extracted:', array.slice(-1), 'array:', array);
    // Elements extracted: [ 5 ] array: [ 1, 2, 3, 4, 5 ]

    More examples in MDN Slice examples

    Performance comparison

    Don't take this as absolute truth as depending on each scenario one might be performant than the other.
    Performance test

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

    Most answers are too wordy.

    • splice and slice return rest of elements in the array.
    • splice mutates the array being operated with elements removed while slice not.

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

    The difference between Slice() and Splice() javascript build-in functions is, Slice returns removed item but did not change the original array ; like,

            // (original Array)
            let array=[1,2,3,4,5] 
            let index= array.indexOf(4)
             // index=3
            let result=array.slice(index)
            // result=4  
            // after slicing=>  array =[1,2,3,4,5]  (same as original array)
    

    but in splice() case it affects original array; like,

             // (original Array)
            let array=[1,2,3,4,5] 
            let index= array.indexOf(4)
             // index=3
            let result=array.splice(index)
            // result=4  
            // after splicing array =[1,2,3,5]  (splicing affects original array)
    
    0 讨论(0)
  • 2020-11-28 18:14

    splice & delete Array item by index

    index = 2

    //splice & will modify the origin array
    const arr1 = [1,2,3,4,5];
    //slice & won't modify the origin array
    const arr2 = [1,2,3,4,5]
    
    console.log("----before-----");
    console.log(arr1.splice(2, 1));
    console.log(arr2.slice(2, 1));
    
    console.log("----after-----");
    console.log(arr1);
    console.log(arr2);
    

    let log = console.log;
    
    //splice & will modify the origin array
    const arr1 = [1,2,3,4,5];
    
    //slice & won't modify the origin array
    const arr2 = [1,2,3,4,5]
    
    log("----before-----");
    log(arr1.splice(2, 1));
    log(arr2.slice(2, 1));
    
    log("----after-----");
    log(arr1);
    log(arr2);

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

    Another example:

    [2,4,8].splice(1, 2) -> returns [4, 8], original array is [2]
    
    [2,4,8].slice(1, 2) -> returns 4, original array is [2,4,8]
    
    0 讨论(0)
提交回复
热议问题