splice(start, ?deleteCount) The function is used to get one or more elements in an array and remove the selected elements from the array.
slice(?start, ?end) slice retreive selected elements without change array.
for example i have this array :
const months = ['Jan', 'March', 'April', 'June'];
let selectedElementsWithSlice = months.slice(2, 3);
//["April"]
// months = ['Jan', 'March', 'April', 'June']
let selectedElements = months.splice(2, 1); // removed from array months
//["April"]
// months = ['Jan', 'March', 'June']
For your subject you want copy element to other array.
You have a multiples solutions :
- for first element selected (check if contains if necessary)
array.push(months.slice(2, 3)[0]);
- For all elements selected
[ ... array, ... months.slice(2, 3) ]
References :
splice documentation : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
slice documentation :
https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/slice