Dan H. Dan H. - 10 months ago 27
Javascript Question

jQuery: Convert Attribute Values to Nested Array (Strings to Numbers)

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() {


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() {