var columns = $ ('# dynamicTable'). ...">

JQuery create int Array from a 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 is the best way to do this?

+4
source share
2 answers

You can use $.parseJSON .

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

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

Here's another way using a while with a 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]; } 
+9
source
 var columns = $('#dynamicTable').attr('columns'), attributeIds = $.map(columns.split(','), function(val, idx) { return parseInt(val, 10) }); 
+7
source

Source: https://habr.com/ru/post/1333478/


All Articles