How to splice an array out of a nested array - javascript
10,832
The MDN says Array.prototype.splice
:
Returns
An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
So, it won't return just the deleted element, it will be wrapped in an array.
Comments
-
Afs35mm almost 2 years
I am attempting to splice a nested array out of its parent array. Consider the following array.
items.splice(0,1)
should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:var items = [[1,2],[3,4],[5,6]]; var item = items.splice(0,1); // should slice first array out of items array console.log(item); //should log [1,2], instead logs [[1,2]]
However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do
item[0]
. What in the world am I missing!? -
Afs35mm over 8 yearsKind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)
-
dpdearing almost 3 yearsBut this would leave
items
in a funky state:[ [], [3, 4], [5, 6] ]