unshift

  • function
can.List.prototype.unshift  

Add elements to the beginning of a List.

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.