admin管理员组文章数量:1429884
I have an array that contains variables and functions. The array is 80 elements long. The first 20 elements are used together in a for loop. When the loop has pleted, the first twenty elements are moved to the back of the array, and the for loop starts again.
I am rebuilding the array this way:
var a2=[the array with 80 elements];
run(a2);
function run(array){
var n=array.slice(0,20); array.splice(0,20);
var con=array.concat(n); a2=con;
}
So I am basically indexing the (new) sliced array, re-indexing the (original) array after the splice, indexing a (new) array after the concat, and re-indexing the original again when I set it equal to the concat. This seems like it is too inefficient. Is there a more established approach to this?
I have an array that contains variables and functions. The array is 80 elements long. The first 20 elements are used together in a for loop. When the loop has pleted, the first twenty elements are moved to the back of the array, and the for loop starts again.
I am rebuilding the array this way:
var a2=[the array with 80 elements];
run(a2);
function run(array){
var n=array.slice(0,20); array.splice(0,20);
var con=array.concat(n); a2=con;
}
So I am basically indexing the (new) sliced array, re-indexing the (original) array after the splice, indexing a (new) array after the concat, and re-indexing the original again when I set it equal to the concat. This seems like it is too inefficient. Is there a more established approach to this?
Share edited Dec 31, 2012 at 20:23 William Smith asked Dec 31, 2012 at 20:05 William SmithWilliam Smith 8521 gold badge13 silver badges23 bronze badges 4- You could chain a little more :-) – John Dvorak Commented Dec 31, 2012 at 20:10
-
What is
a2
? An implicit global? – John Dvorak Commented Dec 31, 2012 at 20:11 - sorry about that. a2 =[the array] – William Smith Commented Dec 31, 2012 at 20:15
-
Do you realise you are breaking encapsulation this way? You assume the argument is
a2
. Either don't assume this, or enforce this. – John Dvorak Commented Dec 31, 2012 at 20:25
2 Answers
Reset to default 7You don't need to slice()
and then splice()
. Splice()
returns the removed elements, so you just need to do that:
var n = array.splice(0, 20);
a2 = array.concat(n);
To be pletely clear, JavaScript's splice()
method returns the removed elements, not the remaining elements.
Also, using globals is generally a bad idea, but you are also kinda mixing them in a weird way. If you are going to keep the variable global, I would either pass the original in as a parameter and return the result from the function:
var a2=[the array with 80 elements];
a2 = run(a2);
function run(array){
var n = array.splice(0, 20);
return array.concat(n);
}
OR
don't pass it in at all and just reference the global from the get-go:
var a2=[the array with 80 elements];
run();
function run(){
var n = a2.splice(0, 20);
a2 = a2.concat(n);
}
You wanted efficiency, so here it is: http://jsfiddle/Hvcbj/
//This is tightly coupled to work for the first 20 of an 80 length array
function swapTwenty( array )
{
for( var i = 0, max = 20; i < max; i++ )
{
var temp = array[i];
for( var n = 1, len = 4; n < len; n++)
{
var base = (i + 20 * (n - 1)) % 80;
var tar = (i + 20 * n) % 80;
array[base] = array[tar];
array[tar] = temp;
}
}
}
EDIT
I had assumed that by swapping in place instead of creating more arrays time would be saved, but in fact I do not think it is. Although this may have a smaller footprint on memory, it does not run faster than the accepted answer. Here is a jsperf showing the difference: http://jsperf./array-modifications
本文标签: javascriptmoving elements in an array from front to backStack Overflow
版权声明:本文标题:Javascript, moving elements in an array from front to back - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744818049a2626818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论