Sergio
Sergio

Reputation: 822

jquery convert num string to int

How can I take a sting containing numbers such as

1 - 2 - 3 - 4 - 5 - 6

and convert each number into a integer?

I have tried the following, but it just returns the first integer.

var a = '1 - 2 - 3 - 4 - 5 - 6';
var b = parseInt( a.split('-') );

$('#b').append(b);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id='b'></div>

Upvotes: 4

Views: 15836

Answers (5)

Maksym Bodnar
Maksym Bodnar

Reputation: 125

This is because the split() function return array and parseInt() not support array in input.

You have to parse each element of array one by one.

Try with this:

var a = '1 - 2 - 3 - 4 - 5 - 6';
// Split string into array
var b = a.split('-');

var c = [];

// convert from string to integer and push it into c array
b.forEach(function (item, index, arr) {
   c.push(parseInt(item.trim()));
});

$('#b').append(c);

Upvotes: 0

Timothy Groote
Timothy Groote

Reputation: 8643

That is because string.split returns an array of strings.

If you want to handle each individual value returned by split, loop over the array and parse the items as numbers while iterating.

You can then do with the parsed number as you will. (in this example, it multiplies every number by two and appends the output)

var a = '1 - 2 - 3 - 4 - 5 - 6';
var splitarray = a.split('-')
    
for(var i=0; i < splitarray.length; i++)
{
  var valueAsInt = parseInt(splitarray[i]);
  //do whatever you want with valueAsInt, like for instance
  valueAsInt *= 2;

  //adding the br so we can see what the individual numbers are
  $('#resultDiv').append(valueAsInt + "<br />"); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="resultDiv"></div>

Upvotes: 4

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

You should split your string based on - and then iterate over it to append each element to your html element

var a = '1 - 2 - 3 - 4 - 5 - 6';
var arr = a.split(' - ');
var ele=$('#b');
for(var i=0; i < arr.length; i++)
{
    ele.append(parseInt(arr[i]));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id='b'></div>

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074208

There's no point in your case, as it's converted back to a string when you append it anyway. On the numbers you've supplied, the conversion will make no difference at all.

In case you have ones where it will matter, see comments:

var a = '1 - 2 - 3 - 4 - 5 - 6';
// Let's not look this up every time
var b = $("#b");
// Split and loop through the parts
a.split('-').forEach(function(entry) {
  // Append each part after converting to int.
  // Note the 10 at the end: That's the number base.
  b.append(parseInt(entry, 10));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id='b'></div>

Upvotes: 1

fdomn-m
fdomn-m

Reputation: 28611

parseInt will convert a single string, not an array of strings.

You can use jquery $.each to parse each item and return an array of int.

(putting this into html as in the question and the snippet below doesn't really mean much as it will convert back to string for the html, but the values can be manipulated once in the array).

var a = '1 - 2 - 3 - 4 - 5 - 6';
var arr = $.each(a.split('-'), function() { return parseInt(this, 10); });

var a = '1 - 2 - 3 - 4 - 5 - 6';
var b = $.each(a.split('-'), function() { return parseInt(this, 10); });
// b is now an array of ints
$("#result").html(b.join(","))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='result'>
</div>

Upvotes: 2

Related Questions