JavaScript Array splice vs slice

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

    The slice() method returns a copy of a portion of an array into a new array object.

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

    This does NOT change the participantForms array but returns a new array containing the single element found at the index position in the original array.

    The splice() method changes the content of an array by removing existing elements and/or adding new elements.

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

    This will remove one element from the participantForms array at the index position.

    These are the Javascript native functions, AngularJS has nothing to do with them.

提交回复
热议问题