Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery create int Array from string of numbers

<div id="dynamicTable" columns="26,40,41,21,71,39,23,19">

var columns = $('#dynamicTable').attr('columns');
var attributeIds = new Array();
attributeIds = columns.split(',');

This creates an array of strings, I need them to be ints. What's the best way to do this?

like image 572
Mark Avatar asked Dec 05 '22 00:12

Mark


1 Answers

You could use $.parseJSON.

Example: http://jsfiddle.net/ULkXy/

var columns = $('#dynamicTable').attr('columns');
var attributeIds = $.parseJSON( '[' + columns + ']' );

Here's another way using a while loop with the unary + operator:

Example: http://jsfiddle.net/ULkXy/1/

var columns = $('#dynamicTable').attr('columns');
var attributeIds = columns.split(',');
var len = attributeIds.length;

while( len-- ) {
    attributeIds[len] = +attributeIds[len];
}
like image 110
user113716 Avatar answered Dec 16 '22 20:12

user113716