# Slice and Splice - And all things nice

Tell us what’s happening:

Hi Guys,

I see lots of people have gone for For Loops to cycle through the array. Is this necessary, if so please could you explain why. All help much appreciated

Regards,

Danny

``````
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!

var arr3 = [...arr1] // I get slice() does not alter original though this is a precaution
var arr4 = [...arr2] // So original is not altertered

// Does this method of equating them affect the original? if not can you say arr4 = ([...arr2])

var arr3Slice = arr3.slice(0, (arr3.length - 1)); // Gut the contents of the first array
var arr4Splice = arr4.splice(n,0, arr3Slice); // At n index Frankentein the guts into it
// If splice does not like arr3Slice could you use the spread operator to put it in here?
return arr4Splice;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36`.

So here’s the thing: setting `arr4Splice` to `arr4.splice(n, 0, arr3)` doesn’t do what you think it’s doing. You’re setting `arr4Splice` to the returned value of that `splice()` call. Splice returns the deleted items, or an empty array.

You don’t need to for/next, in this case. In fact, it will make things WAY more confusing.

You’re right to create a new array, `arr4`, as `arr4.splice(...)` modifies that array IN PLACE. But as `arr1` is not being modified in any way, simply spread that value into the `arr4.splice(...)`, and `return arr4`.

Hope that helps. Looking back over that I just confused the CRAP outta myself.

Hello again Snowmonkey, thanks for taking the time to stop by

I thought splice returns you the full array with the new additions? I thought slice was the one that returns you the copied items?

“So here’s the thing: setting arr4Splice to arr4.splice(n, 0, arr3) doesn’t do what you think it’s doing. You’re setting arr4Splice to the returned value of that splice() call. Splice returns the deleted items, or an empty array.”

I have altered the code below. Still a bit lost.

When you insert a full array such as arr1 are you interesting a full array or will the number enter the second array
[1, 2, 3, 4] into [5, 6, 7, 8] at index 2 [ 5, 6, 1, 2, 3, 4, 7, 8] or [5, 6, [1, 2, 3, 4], 7,8]

If first example is correct why can we no do the following

``````function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!

var arr4 = [...arr2] // So original is not altertered

var arr4Splice = arr4.splice(n,0, arr1);

return arr4Splice;
}

``````

Slice doesn’t affect the original array, and it returns the SELECTED elements.

Splice DOES affect the original array, but returns the elements DELETED from that array.

Doing the code you now have in your example will insert the ARRAY into the array – but `splice(...)` wants a LIST like so: `arr4.splice(n, 0, val1, val2, val3, val4)` – so using the spread operator on `arr11` in the `splice()` call will convert that from an array to a list of separate values.

1 Like