2

I've got buttons with a common class (buttons). How can I add their IDs in to an array in a reverse order?

var yourArray = [];
$('.buttons').each(function() {
     yourArray.push( $(this).prop('id') );
});
5
  • array.unshift()? Commented Apr 22, 2016 at 8:07
  • Add yourArray.reverse(); outside the loop Commented Apr 22, 2016 at 8:08
  • @Mihai - doesn't that just reverse an already created array? unshift at least allows one to create an array from back to front ("in reverse order") Commented Apr 22, 2016 at 8:09
  • Yes but why does it matter @evolutionxbox Commented Apr 22, 2016 at 8:09
  • Flexibility. OP might have more code which prevents a reverse... Commented Apr 22, 2016 at 8:10

2 Answers 2

4

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

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

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() {
    return this.id;
}).get().reverse();

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

Sign up to request clarification or add additional context in comments.

4 Comments

@Becky A POJS array doesn't have an each() method. You should use a normal for loop or $.each(yourArray, function() { ... });
Is it possible to reverse the order within the existing each() function? $('.buttons').each(function(){
I'm bit lost. Can you please help? $('.buttons).each(function(){ yourArray.unshift(this.id); var data = $(this).text().replace(/\s*$/g, ''); }); How do I reverse the order for $(this).text(). ?
Could you setup a new fiddle for this as I can't quite get what you're trying to do with the text(). It may be worth starting a new question if it's a different issue to the above.
2

You can do something simple with map() and reverse()

var yourArray =  $('.buttons').map(function() {
  return this.id; //  get the id
})
.get() // get the array
.reverse(); // reverse the array

console.log(yourArray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="a" class="buttons"></button>
<button id="b" class="buttons"></button>

4 Comments

How should I use the newly create array if I need to use it in another `each(function(){' ?
yourArray.forEach(function(v){........................................}); , use forEach or $.each()
Thanks. I want to use it in an existing each() function. I tried yourArray.each(function(){...}); doesn't seems to work.
@Becky : it's only work with jquery object, not with array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.