0

I'm trying to splice array two from the two-dimensional array arr.

var one = ["a","b"];  
var two = ["c","d"];  
var thr = ["e","f"];  
var arr = [one,two,thr];

Here are my two unsuccessful attempts:

1)

var rem = arr.splice(1,1);  
alert(rem[0]);  
// This alerts the entire array as one long string "c,d".
// The expected behavior is "c" (index 0 of the returned array _rem_).

2)

var rem = arr[1].splice(0);  
alert(arr);  
// The array _rem_ is now correct, but _arr_ is left with an empty index: "a,b,,e,f".

I have a less than ideal workaround, but I'm hoping to accomplish this with one function.

var test = arr[1].slice(0);  
arr.splice(1,1);
4
  • I think you may have tested incorrectly or something? I can't reproduce this. arr.splice(1,1) should work for you. Here's a fiddle of a working version. jsfiddle.net/pNgTh Commented May 1, 2011 at 4:10
  • What's a "string vice array"? Commented May 1, 2011 at 4:13
  • Haha, vice as in "a string instead of an array". Commented May 1, 2011 at 4:18
  • Sorry 'arr.splice(1,1)' is what I was using and meant to put for the first example. Here is the problem, set the output of arr.splice(1,1) to a variable. Then alert index 0 of that output. Ex: test=arr.splice(1,1); alert(test[0]); You'll see that it's not index 0 of the array, it shows the entire array. I updated your fiddle to display this. Commented May 1, 2011 at 6:29

3 Answers 3

5

Interesting observation. From the ECMA-specs (262, ed. 5):

When the splice method is called with two or more arguments start, deleteCount and (optionally) item1, item2, etc., the deleteCount elements of the array starting at array index start are replaced by the arguments item1, item2, etc. An Array object containing the deleted elements (if any) is returned.

Array.splice thus returns an array of the removed elements. So, if you want to reference the removed array, this should be the syntax:

var rem = arr.splice(1,1)[0];
alert(rem[0]); //=> 'c'
Sign up to request clarification or add additional context in comments.

2 Comments

Outstanding, that is exactly what I was looking for. Thanks!
I wasted an hour on this before I came across your solution. Thanks!
1

You seem confused, possibly by what happens when you pass an array to alert(). From what I can tell of what you want, your first example is correct. Before doing any splicing, what you have in your arr variable is

[
   ["a", "b"],
   ["c", "d"],
   ["e", "f"]
]

After calling var rem = arr.splice(1, 1), which removes one element from arr at index 1 and stores it in an array in variable rem, what you have is

arr:

[
   ["a", "b"],
   ["e", "f"]
]

rem:

[
   ["c", "d"]
]

So rem[0] is the array ["c", "d"], which is what I thought you wanted.

Comments

0

I think its working as expected splice method always returns an array.

when you say arr[1].splice(0); its calling spilce on one and gives ['a']

And when you do arr.splice(1,1); it will return [one]

7 Comments

arr.splice(1,1) splices the correct array and removes the element as expected. The problem is, what's returned is not an array. alert(arr.splice(1,1)[0]) shows "c,d" not "c" as expected.
yes, its because the splice always returns an array. like, var arr = [1,2,3,4] and arr.slice(1,1) will be [1]
That's not what's happening... arr.slice(1,1) should be an array, but if we try to look at index 0 of what's returned, we see that an array wasn't returned, but a string of every element of the array concatenated.
this is because you are using alert to view them, use console.log to see the returned values. you will find it as array. What I've seen in firebug in firefox that if you alert an array it shows as string in alert box.. but when you do console.log it shows array. so what I've observed is alert([1,2,3,4]) will show 1,2,3,4 in alert box
Yes, if you alert the array it will show the whole thing as a string. Not if you alert an index of the array. If you have myarray = [1,2,3] and alert(myarray[0]) the output will only be '1'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.