What is the difference between splice
and slice
?
$scope.participantForms.splice(index, 1);
$scope.participantForms.slice(index, 1);
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/
Syntax
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Parameters
start
: required. Initial index.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
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
Syntax
array.slice([begin[, end]])
Parameters
begin
: optional. Initial index (default 0).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
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
Don't take this as absolute truth as depending on each scenario one might be performant than the other.
Performance test
Most answers are too wordy.
splice
andslice
return rest of elements in the array.splice
mutates the array being operated with elements removed whileslice
not.
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)
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);
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]