Becky Becky - 11 months ago 60
Javascript Question

create array in reverse order

I've got buttons with a common class (

). How can I add their
s in to an array in a reverse order?

var yourArray = [];
$('.buttons').each(function() {
yourArray.push( $(this).prop('id') );


You could create the array by adding each element to the beginning of the array using unshift():

var yourArray = [];
$('.buttons').each(function() {

Alternatively you can create it in the current order and then reverse() it. Also note that you can use map() to create the array initially:

var yourArray = $('.buttons').map(function() {

Finally you can use instead of creating a jQuery object just to access a property already accessible without the need of object creation.