peter vries
peter vries

Reputation: 173

Jquery making array within an array

I'm trying to create an array in jquery. I need this array to have multiple other arrays in them.

My code now:

      var arr = [];

     $('.thumb').each(function(){
      arr.push($(this).attr('data-storename'),$(this).attr('data-grid-item-id')); 
 });

This gives me just 1 array with all the data-storename's and data-grid-item-id's in it.

I want my array to look like :

0 [

   0 =>   data-storename : (someinfo)
   1 =>   data-grid-item-id : (someinfo)
]


1 [

   0 =>   data-storename : (someinfo)
   1 =>   data-grid-item-id : (someinfo)
]

and so on.

All my attempts end up being one single array, but just nested inside another array. Any help?

Upvotes: 1

Views: 73

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

Firstly you can use map() to create the outer array. You can then return an array containing the two values you require from the map() handler function. Try this:

var arr = $('.thumb').map(function(){
    return [[$(this).data('storename'), $(this).data('grid-item-id')]];
}).get()

Working example

Upvotes: 2

Related Questions