What is the difference between splice
and slice
?
$scope.participantForms.splice(index, 1);
$scope.participantForms.slice(index, 1);
See Question&Answers more detail:osWhat is the difference between splice
and slice
?
$scope.participantForms.splice(index, 1);
$scope.participantForms.slice(index, 1);
See Question&Answers more detail:ossplice()
changes the original array whereas slice()
doesn't but both of them returns array object.
See the examples below:
var array=[1,2,3,4,5];
console.log(array.splice(2));
This will return [3,4,5]
. The original array is affected resulting in array
being [1,2]
.
var array=[1,2,3,4,5]
console.log(array.slice(2));
This will return [3,4,5]
. The original array is NOT affected with resulting in array
being [1,2,3,4,5]
.
Below is simple fiddle which confirms this:
//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);