JavaScript Array splice vs slice

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

提交回复
热议问题