DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • prototype
    • concat
    • forEach
    • indexOf
    • join
    • pop
    • push
    • replace
    • reverse
    • shift
    • slice
    • splice
    • unshift
  • static
    • can.List.Map
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • can.List
  • /
  • unshift
  • / On this page
    • unshift

      function

      Add elements to the beginning of a List.

      • source

      list.unshift(...elements)

      unshift adds elements onto the beginning of a List.

      Parameters

      1. elements {*}:

        the elements to add to the List

      Returns

      {Number}:

      the new length of the List

      unshift adds elements to the front of the list in bulk in the order specified:

      var list = new can.List(['Alice']);
      
      list.unshift('Bob', 'Eve');
      list.attr(); // ['Bob', 'Eve', 'Alice']
      

      If you have an array you want to concatenate to the beginning of the List, you can use apply:

      var names = ['Bob', 'Eve'],
          list = new can.List(['Alice']);
      
      list.unshift.apply(list, names);
      list.attr(); // ['Bob', 'Eve', 'Alice']
      

      Events

      unshift causes change, add, and length events to be fired.

      See also

      unshift has a counterpart in shift, or you may be looking for push and its counterpart pop.

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.