

Examples Return a portion of an existing arrayĬopy Code var unboundSlice = If a new element is added to either array, the other array is not affected. Changes to the string, number or boolean in one array does not affect the other array.

For object references (and not the actual object), slice copies object references into the new array.Elements of the original array are copied into the returned array as follows: It returns a shallow copy of elements from the original array.

Return valueĪ new array containing the extracted elements. If end is omitted, slice extracts through the end of the sequence ( arr.length). slice(2,-1) extracts the third element through the second-to-last element in the sequence. As a negative index, end indicates an offset from the end of the sequence. slice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3). slice extracts up to but not including end. end Zero-based index at which to end extraction. If begin is undefined, slice begins from index 0. slice(-2) extracts the last two elements in the sequence. As a negative index, begin indicates an offset from the end of the sequence. slice(]) Parameters begin Zero-based index at which to begin extraction. Increment the index after performing the splice.Arr. For each item in the first array splice it into the copied array in the index given as argument. Loop through all of the items in the first array. This can be done by using the slice operation on the second array, and assign it to a variable.

This will ensure that the original array is not mutated. Relevant LinksĬreate a copy of the second array inside of the function. That is, we cannot make any changes to the original arrays. We’ve also got to ensure that the original arrays are not mutated. We need to copy each element from the first array into the second array starting at the index n.
