Dan H. Dan H. - 2 years ago 76
Javascript Question

jQuery: attribute values into a nested array

Using jQuery, I would like to get each attribute value, insert it into an array and then insert each array into an array.

From this HTML:

<li data-bbox="-121,20,-36,30">Item 1</li>
<li data-bbox="-122,30,-46,40">Item 2</li>
<li data-bbox="-123,40,-56,50">Item 3</li>

I'm trying to create this type of nested array:

var bboxArray = [

I'm assuming I need to do something like this:

var bboxArray = [];
$('li[data-bbox]').each(function() {

However, this is just creating one large array. What am I missing?

Answer Source

Working Example

While your code does work, it is returning strings instead of the numbers you have in your required output this will do that:

I simply added .map(Number) at the end of your push

$('li[data-bbox]').each(function() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download