56

I have a string that's on the page and from which I want an array of int.

<div id="TheData">2,3,0,43,23,53</div>

I'm writing this:

var ArrayData = ($('#TheData').html()).split(',');

However, ArrayData becomes an array of strings. How can I get an array of ints? Note that some of the elements in the HTML can be equal to 0.

Thanks.

1

6 Answers 6

72
var ArrayData = $('#TheData').html().split(',').map( Number );

Add Array.prototype.map() to older browsers with the code from MDN.


You can use jQuery's $.map() in the same manner, though it won't work with $.prototype.map().

var ArrayData = $.map( $('#TheData').html().split(','), Number );
Sign up to request clarification or add additional context in comments.

3 Comments

This will only return integers if there are only strings that convert to integers in the array.
@kennebec: Well, yes. I'm afraid I don't understand the point you're making.
If the end-user alters the text to include decimals, you could end up with floats instead of ints. That may be okay in some scenarios, but it could cause unexpected results for those not expecting them.
42
var ArrayData = $.map($('#TheData').text().split(','), function(value){
    return parseInt(value, 10);
    // or return +value; which handles float values as well
});

You can use $.map to transform the array of strings to ints by calling parseInt on each of the elements in the array

7 Comments

It should be $.map($('#TheData').text().split(','), function(){...});.
Note: This isn't an example of jQuery.map, but of Array#map.
This returned an array of NaN, needed to add function (n) ... parseInt(n, 10).
Sorry about that guys @FelixKling thank you for setting me straight
@frenchie Check out the compatibility implementation for support of Array#map in older browsers. Or the ES5 shim for a collection of them.
|
29

Pure Javascript solution:

const elementText = document.getElementById('divSourceID').innerText;
const numericList = elementText.split(',').map(Number);

For more information:

  1. getElementById: "The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly. (...)". Source: developer.mozilla.org.

  2. Array.prototype.map: "The map() method creates a new array populated with the results of calling a provided function on every element in the calling array". Source: developer.mozilla.org.

  3. array.map(Number): This call means the first received argument will be automatically converted into number and results in the same as if you explicitly declare the arrow function:

const numericList = elementText.split(',').map(Number);

same result as:

const numericList = elementText.split(',').map(str => Number(str));

JIT: Special thanks to @Robbendebiene for the excellent code review, simplifying the previous code.

2 Comments

This function can be simplified to: elementText.split(',').map(Number);
Excellent point @Robendebiene, thank you. I will update my answer as some of the code I wrote as not necessary (split already returns an array, for example). Good catch. Thank you.
14
var ArrayData = $('#TheData').text().split(',').map(Number);

You can find more here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Comments

9

Here is a simple answer

let x = "1,2,3,4";

let result = x.split(",").map((e) => parseInt(e));

console.log(result);

1 Comment

Easy to understand and does the job!
2
var ArrayData = $('#TheData').html().split(',').map( d => { return parseInt(d) });

Use map function after Split, in callback of map you can parse that Integer

1 Comment

Hi, welcome to the site! My apologies if I'm missing something, but isn't your answer pretty much what was already posted as the accepted answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.