Python port code for javascript
2 answers
I'm new to Python, but if I understand the code correctly, it restores the list from a given offset to every element following offset + 1 and an offset element.
The run 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, you can write:
indices = indices.concat( indices.splice( i, 1 ) );
The whole sequence goes:
>>> 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 destroys the array, but returns the removed elements, which can then be passed to concat .
+6
a source to share
You need 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];
}
+1
a source to share