如何在 JavaScript 中的数组开头添加新的数组元素?
我需要在数组的开头添加或添加元素。
例如,如果我的数组如下所示:
[23, 45, 12, 67]
并且我的 AJAX 调用的响应是 34
,我希望更新后的数组如下所示:
[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 是否有任何内置功能可以做到这一点?
我的方法的复杂度是O(n)
,看到更好的实现将会非常有趣。
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]
And the response from my AJAX call is 34
, I want the updated array to be like the following:
[34, 23, 45, 12, 67]
Currently I am planning to do it like this:
var newArray = [];
newArray.push(response);
for (var i = 0; i < theArray.length; i++) {
newArray.push(theArray[i]);
}
theArray = newArray;
delete newArray;
Is there a better way to do this? Does JavaScript have any built-in functionality that does this?
The complexity of my method is O(n)
and it would be really interesting to see better implementations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用
unshift
。它类似于push
,只不过它将元素添加到数组的开头而不是结尾。unshift
/push
- 将元素添加到数组的开头/结尾shift
/pop
- 删除并返回数组的第一个/最后一个元素一个简单的图表...
和图表:
push
pop
取消移动
移动
查看 MDN 数组文档。实际上,每种能够从数组中推送/弹出元素的语言也都能够取消移位/移位(有时称为
push_front
/pop_front
)元素,但你永远不应该必须自己实施这些。正如评论中指出的,如果您想避免改变原始数组,可以使用
concat
,将两个或多个数组连接在一起。您可以使用它在功能上将单个元素推送到现有数组的前面或后面;为此,您需要将新元素转换为单个元素数组:concat
也可以追加项目。concat
的参数可以是任何类型;如果它们还不是数组,则它们隐式包装在单元素数组中:Use
unshift
. It's likepush
, except it adds elements to the beginning of the array instead of the end.unshift
/push
- add an element to the beginning/end of an arrayshift
/pop
- remove and return the first/last element of an arrayA simple diagram...
and chart:
push
pop
unshift
shift
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.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:concat
can also append items. The arguments toconcat
can be of any type; they are implicitly wrapped in a single-element array, if they are not already an array:对于 ES6,使用展开运算符
...
:演示
With ES6, use the spread operator
...
:Demo
另一种方法是通过
concat
:concat
和unshift
之间的区别在于concat
返回一个新数组。它们之间的性能可以在此处找到。这是测试结果:
Another way to do that is through
concat
:The difference between
concat
andunshift
is thatconcat
returns a new array. The performance between them could be found here.Here is the test result:
快速备忘单:
术语“shift/unshift”和“push/pop”可能有点令人困惑,至少对于不熟悉 C 语言编程的人来说
是这样。如果您不熟悉术语,请参阅以下内容替代术语的快速翻译,可能更容易记住:
Quick Cheatsheet:
The terms shift/unshift and push/pop can be a bit confusing, at least to folks who may not be familiar with programming in C.
If you are not familiar with the lingo, here is a quick translation of alternate terms, which may be easier to remember:
使用 ES6 解构(避免原始数组发生突变):
const newArr = [项目, ...oldArr]
Using ES6 destructuring (avoiding mutation off the original array):
const newArr = [item, ...oldArr]
您有一个数组: var arr = [23, 45, 12, 67];
要将项目添加到开头,您需要使用 splice:
You have an array:
var arr = [23, 45, 12, 67];
To add an item to the beginning, you want to use
splice
:没有变异
实际上,所有
unshift
/push
和shift
/pop
改变源数组。unshift
/push
从 begin/end 向现有数组添加一个项目,shift
/pop
删除一个项目从数组的开头/结尾开始。但是,几乎没有什么方法可以在不进行突变的情况下将项目添加到数组中。结果是一个新数组,要添加到数组的末尾,请使用以下代码:
要添加到原始数组的开头,请使用以下代码:
通过上述方式,您可以添加到数组的开头/结尾而不进行突变。
Without Mutating
Actually, all
unshift
/push
andshift
/pop
mutate the source array.The
unshift
/push
add an item to the existed array from begin/end andshift
/pop
remove an item from the beginning/end of an array.But there are few ways to add items to an array without a mutation. the result is a new array, to add to the end of array use below code:
To add to begin of original array use below code:
With the above way, you add to the beginning/end of an array without a mutation.
在数组中添加新元素的备忘单
1.
Array#unshift
< /a>2.
Array#splice
< /a>3. ES6 传播...
4.
Array#concat
< /a>注意:在每个示例中,您可以通过提供更多要插入的项目来添加多个项目。
Cheatsheet to prepend new element(s) into the array
1.
Array#unshift
2.
Array#splice
3. ES6 spread...
4.
Array#concat
Note: In each of these examples, you can prepend multiple items by providing more items to insert.
如果需要在数组开头连续插入元素,使用
push
语句并随后调用reverse
会比调用unshift 更快一直以来。
基准测试: http://jsben.ch/kLIYf
If you need to continuously insert an element at the beginning of an array, it is faster to use
push
statements followed by a call toreverse
, instead of callingunshift
all the time.Benchmark test: http://jsben.ch/kLIYf
使用 splice 我们在数组的开头插入一个元素:
Using
splice
we insert an element to an array at the begnning:如果要将数组中的元素推送到数组开头,请使用
.apply(,)
:If you want to push elements that are in an array at the beginning of your array, use
<func>.apply(<this>, <Array of args>)
: