Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

javascript - 如何在Javascript数组的开头添加新的数组元素?(How can I add new array elements at the beginning of an array in 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] And the response from my AJAX call is 34 , I want the updated array to be like the following:(我的AJAX调用的响应为34 ,我希望更新后的数组如下所示:) [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 any better way to do this?(有什么更好的方法吗?) Does Javascript have any built-in functionality that does this?(Javascript是否具有执行此操作的任何内置功能?) The complexity of my method is O(n) and it would be really interesting to see better implementations.(我的方法的复杂度为O(n) ,看到更好的实现将真的很有趣。)   ask by Moon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use unshift .(使用unshift 。)

It's like push , except it adds elements to the beginning of the array instead of the end.(就像push一样,除了将元素添加到数组的开头而不是结尾。) unshift / push - add an element to the beginning/end of an array(unshift / push -一个元素添加到一个数组的开始/结束) shift / pop - remove and return the first/last element of an array(shift / pop删除并返回数组的第一个/最后一个元素) A simple diagram...(一个简单的图...) unshift -> array <- push shift <- array -> pop and chart:(和图表:) add remove start end push X X pop X X unshift X X shift X X Check out the MDN Array documentation .(查看MDN阵列文档 。) 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.(实际上,每种能够从数组中推入/弹出元素的语言都将具有取消/移入(有时称为push_front / pop_front )元素的能力,您永远不必自己实现这些元素。) 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.(如注释中所指出的那样,如果要避免更改原始数组,可以使用concat ,它将两个或多个数组连接在一起。) 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 ] concat can also append items.(concat也可以附加项目。) The arguments to concat can be of any type;(concat的参数可以是任何类型。) 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 ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...