views:

181

answers:

2
indices[i:] = indices[i+1:] + indices[i:i+1]

Hope someone helps.

+1  A: 

You will want to look at Array.slice()

var temp=indices.slice(i+1).concat(indices.slice(i, i+1));
var arr=[];
for (var j=0; j<temp.length; j++){
   arr[j+i]=temp[i];
}
Lenni
+5  A: 

I'm fairly new to Python but if I understand the code correctly, it reconstructs a list from a given offset into every item following offset+1 and the item at the offset.

Running it seems to confirm this:

>>> indices = ['one','two','three','four','five','six']
>>> i = 2
>>> indices[i:] = indices[i+1:] + indices[i:i+1]
>>> indices
['one', 'two', 'four', 'five', 'six', 'three']

In Javascript can be written:

indices = indices.concat( indices.splice( i, 1 ) );

Same entire sequence would go:

>>> var indices = ['one','two','three','four','five','six'];
>>> var i = 2;
>>> indices = indices.concat( indices.splice( i, 1 ) );
>>> indices
["one", "two", "four", "five", "six", "three"]

This works because splice is destructive to the array but returns removed elements, which may then be handed to concat.

Borgar
Is it obvious that the last sentence is contrived just to fit in some links to the methods on MDC? :-)
Borgar
what javascript command line is that?
Paolo Bergantino
It is an imaginary command line. I ran the code with FireBug but added the >>> in the end simply to make it look the same as the Python block. :-)
Borgar
Tricky, tricky. ;)
Paolo Bergantino
@Borgar, I will buy your imaginary command line. How much does it cost, in Quatloos?
Nosredna