How can I add new array elements at the beginning of an array in Javascript?

前端 未结 13 719
一生所求
一生所求 2020-11-22 13:31

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]

13条回答
  •  执念已碎
    2020-11-22 14:02

    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:

    * array_unshift()  -  (aka Prepend ;; InsertBefore ;; InsertAtBegin )     
    * array_shift()    -  (aka UnPrepend ;; RemoveBefore  ;; RemoveFromBegin )
    
    * array_push()     -  (aka Append ;; InsertAfter   ;; InsertAtEnd )     
    * array_pop()      -  (aka UnAppend ;; RemoveAfter   ;; RemoveFromEnd ) 
    

提交回复
热议问题