简体   繁体   English

将第一个元素移动到数组末尾的最快方法

[英]Fastest way to move first element to the end of an Array

I'm wondering what is the fastest way in JavaScript to move an element from the beginning of an Array to the end.我想知道 JavaScript 中将元素从Array的开头移动到结尾的最快方法是什么。 For example if we have例如,如果我们有

[8,1,2,3,4,5,6,7]

And we want: [1,2,3,4,5,6,7,8]我们想要: [1,2,3,4,5,6,7,8]

I want to move the first element to the end.我想将第一个元素移到最后。 I was thinking about switching element 0 with element 1, after that switching element 1 with element 2 and so on until the 8 is at the and (basically how bubblesort works).我正在考虑将元素 0 与元素 1 切换,然后将元素 1 与元素 2 切换,依此类推,直到 8 位于和(基本上是冒泡排序的工作原理)。 I was wondering if there is a faster way to bring the first element to the end.我想知道是否有更快的方法来结束第一个元素。

I will be using small Arrays (around 10 elements), and I want to avoid shift() since it's pretty slow.我将使用小数组(大约 10 个元素),我想避免shift()因为它很慢。

This is what I have now on chrome it's 45% faster than normal shift+push: http://jsperf.com/shift-myfunc这就是我现在在 chrome 上的内容,它比普通的 shift+push 快 45%: http : //jsperf.com/shift-myfunc

The arrays will have objects in them for a game.数组中将包含用于游戏的对象。

var ary = [8,1,2,3,4,5,6,7];
ary.push(ary.shift());  // results in [1, 2, 3, 4, 5, 6, 7, 8] 

jsFiddle example jsFiddle 示例

 var ary = [8,1,2,3,4,5,6,7]; console.log("Before: " + ary); ary.push(ary.shift()); // results in [1, 2, 3, 4, 5, 6, 7, 8] console.log("After: " + ary);

Use shift and push使用shiftpush

var a = ["a","b","c"];
var b = a.shift();
a.push(b);

or

var b = a.shift();
a[a.length] = b;

Edit Based on updated question根据更新的问题编辑

What is going to be the fastest?什么是最快的? Really depends on content of the array and what browser/version!真的取决于数组的内容和浏览器/版本!

Now what are the ways to remove the first index?现在删除第一个索引的方法是什么?

  • shift()
  • splice()
  • slice()

Now what are the ways to add to the last index?现在有什么方法可以添加到最后一个索引?

  • push()
  • array[array.length]
  • concat() -- not even going to try concat() - 甚至不打算尝试

Other ways其他方式

  • for loop - make new array [going to be horrible on large arrays] for 循环 - 创建新数组 [在大数组上会很糟糕]

JSPerf: JSPerf:

http://jsperf.com/test-swapping-of-first-to-last http://jsperf.com/test-swapping-of-first-to-last


What is really the fastest?什么才是最快的?

What is the fastest really depends on what you are doing with the array.最快的实际上取决于您对阵列的处理方式。 If you are just using the first index, it will be fastest just to make your code read an index and not shift the values.如果您只是使用第一个索引,那么让您的代码读取索引而不是移动值将是最快的。 If you are using all the indexes, than just loop and through and when you get to the end start back at zero.如果您正在使用所有索引,则不仅仅是循环和通过,当您到达最后时从零开始。 Basic counters.基本计数器。

And here is a sweet ES6 version这是一个甜蜜的 ES6 版本

let arr = [1,2,3,4,5,6]

const [first, ...rest] = arr;
arr = [...rest,first]

Use splice to get the first element使用 splice 获取第一个元素

var first = array.splice(0,1);

Then push to make if the last.然后推动使如果最后。

Since the return value of the splice method is an array, you have to say既然splice方法的返回值是一个数组,就不得不说

array.push(first[0]);

Working example here: JSFIDDLE这里的工作示例: JSFIDDLE

Just in case you want to put any element to the end:以防万一你想把任何元素放在最后:

var ary = [8,1,2,3,4,5,6,7];
ary.push(ary.splice(position, 1)[0]);

for position just wrap this in a forEach.对于position只需将其包装在 forEach 中即可。

var a = [1,2,3,4,5,6,7,8];
var b= a[7];
var c = a.slice(1, 8);
c.push(b);

Edit: It's probably better to just shift, like epascarello did in his answer .编辑:最好只是转移,就像 epascarello 在他的回答中所做的那样。

With ES20...使用 ES20...

const newArr = [
   ...arr.slice(1),
   arr[0]
];

one more flavour还有一种味道

var arr = [0, 1, 2];
arr = arr.concat(arr.shift())

concat can add not just a element but another array at the end or beginning concat 不仅可以添加一个元素,还可以在末尾或开头添加另一个数组

Updating the arrays moving elements from one extreme to the other:更新将元素从一个极端移动到另一个极端的数组:

const array = ['First', 'Second', 'Third', 'Fourth'];

const next = [...array]
next.push(next.shift())
console.log(next); // [ 'Second', 'Third', 'Fourth', 'First' ]

const prev = [...array]
prev.unshift(prev.pop())
console.log(prev); // [ 'Fourth', 'First', 'Second', 'Third' ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM