English 中文(简体)
Why does splice return a string instead of an array?
原标题:

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);
最佳回答

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 
问题回答

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.

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]





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签