JavaScript Array splice vs slice

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

提交回复
热议问题