![]() ![]() ![]() And you're equipped with a handy mnemonic, that splice compared to slice has an additional letter, 'p', which helps you remember that splice mutates and optionally adds or removes from the original array. You need to recursively clone each element (the way the 'secondary implementation' you posted is doing) if you want a deep clone. So the answer is no: slice by itself is not enough to clone a multidimensional array. You now know that slice makes a shallow copy of the original array, while splice mutates the original array and optionally adds or removes elements. The slice () method returns a shallow copy of a portion of an array into a new array object. end Optional Byte index before which to end slicing. ![]() Try it Syntax slice(begin) slice(begin, end) Parameters begin Zero-based byte index at which to begin slicing. ConclusionĪnd there we have it! This blog goes over the differences between slice and splice. The slice () method returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer s bytes from begin, inclusive, up to end, exclusive. And because splice can add and remove stuff to the original array, that means that it also mutates the original array. ![]() Because of the extra letter, I associate the additional letter to splice's use of adding or removing from the original array. splice has an extra letter, 'p', compared to slice. I remember the difference between slice and splice using a mnemonic. insert 'juliet' and 'zeke' at 3rd index // returns Ĭonsole. splice ( 3, 1, 'juliet', 'zeke' ) // remove 'harper'. ![]()
0 Comments
Leave a Reply. |