如何在 Javascript 中的数组开头添加新的数组元素? [英] How can I add new array elements at the beginning of an array in Javascript?

查看:33
本文介绍了如何在 Javascript 中的数组开头添加新的数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在数组的开头添加或添加元素.

I have a need to add or prepend elements at the beginning of an array.

例如,如果我的数组如下所示:

For example, if my array looks like below:

[23, 45, 12, 67]

我的 AJAX 调用的响应是 34,我希望更新后的数组如下所示:

And the response from my AJAX call is 34, I want the updated array to be like the following:

[34, 23, 45, 12, 67]

目前我打算这样做:

var newArray = [];
newArray.push(response);

for (var i = 0; i < theArray.length; i++) {
    newArray.push(theArray[i]);
}

theArray = newArray;
delete newArray;

有没有更好的方法来做到这一点?Javascript 是否具有执行此操作的任何内置功能?

Is there any better way to do this? Does Javascript have any built-in functionality that does this?

我的方法的复杂度是 O(n),看到更好的实现会很有趣.

The complexity of my method is O(n) and it would be really interesting to see better implementations.

推荐答案

使用 unshift.就像 push,除了它将元素添加到数组的开头而不是结尾.

Use unshift. It's like push, except it adds elements to the beginning of the array instead of the end.

  • unshift/push - 在数组的开头/结尾添加一个元素
  • shift/pop - 删除并返回数组的第一个/最后一个元素
  • unshift/push - add an element to the beginning/end of an array
  • shift/pop - remove and return the first/last element of an array

一个简单的图表...

   unshift -> array <- push
   shift   <- array -> pop
 

和图表:

          add  remove  start  end
   push    X                   X
    pop           X            X
unshift    X             X
  shift           X      X

查看 MDN 阵列文档.几乎每一种能够从数组中推入/弹出元素的语言也都能够取消移动/移动(有时称为 push_front/pop_front)元素,你永远不应该必须自己实现这些.

Check out the MDN Array documentation. Virtually every language that has the ability to push/pop elements from an array will also have the ability to unshift/shift (sometimes called push_front/pop_front) elements, you should never have to implement these yourself.

正如评论中指出的,如果你想避免改变你的原始数组,你可以使用 concat,将两个或多个数组连接在一起.您可以使用它在功能上将单个元素推送到现有数组的前面或后面;为此,您需要将新元素转换为单个元素数组:

As pointed out in the comments, if you want to avoid mutating your original array, you can use concat, which concatenates two or more arrays together. You can use this to functionally push a single element onto the front or back of an existing array; to do so, you need to turn the new element into a single element array:

const array = [3, 2, 1]

const newFirstElement = 4

const newArray = [newFirstElement].concat(array) // [ 4, 3, 2, 1 ]

console.log(newArray);

concat 也可以附加项目.concat 的参数可以是任何类型;如果它们还不是数组,它们将被隐式包装在一个单元素数组中:

concat can also append items. The arguments to concat can be of any type; they are implicitly wrapped in a single-element array, if they are not already an array:

const array = [3, 2, 1]

const newLastElement = 0

// Both of these lines are equivalent:
const newArray1 = array.concat(newLastElement) // [ 3, 2, 1, 0 ]
const newArray2 = array.concat([newLastElement]) // [ 3, 2, 1, 0 ]

console.log(newArray1);
console.log(newArray2);

这篇关于如何在 Javascript 中的数组开头添加新的数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆