0
["57300","730","17300","17330","17340","70","220","320","380","420","340","130","50","280","360","550","43110","44340","400","620","440","20","72850"]

Is an array of strings being passed back into a javascript function which proceeds to do work on it to break it up into an array of strings (using the split function with "," as a delimiter).

The problem here, is I need to convert each of those after that into an integer. Sounds easy okay.

So I proceed to do a:

$.each(data, function(i, item)

On it. If I console log the item, I can see it being "57300", "730" etc etc. Which is fine. But if I try to do this:

var number = parseInt(item, 10);

And console log what number is... its NaN (should it not be 57300, 730, etc without the quotes?). Is some hidden character messing with me causing parseInt to break?

11
  • They seem to be ["\"57300\"","\"730\"", ...] rather than ["57300","730", ...] Commented Nov 28, 2012 at 17:56
  • 4
    unable to recreate: jsfiddle.net/TSJ8J Commented Nov 28, 2012 at 17:56
  • 1
    Please provide the actual code. Why do you need to split? Why is your "array" a string? Don't you decode your JSON data? Commented Nov 28, 2012 at 17:57
  • If you had to use split to break it up, then it wasn't an array of strings, it was just one big string. It sounds like you maybe should be using JSON.Parse. Commented Nov 28, 2012 at 17:58
  • It already is an array of strings. Don't split it. Commented Nov 28, 2012 at 17:59

2 Answers 2

2

I think your initial string is like below,

var result = '"57300","730","17300","17330"';

Since you are splitting with , as delimiter, you will end up getting like below,

data = ["\"57300\"","\"730\"","\"17300\"","\"17330\""]; 

Basically a string with quotes.. And when you parse using parseInt

parseInt("\"57300\"", 10); //throws NaN

Solution:

Instead you can wrap that data with [] and instead of split use parseJSON function. See below,

var result = '"57300","730","17300","17330"';

result = '[' + result + ']';

var data = $.parseJSON(result);

$.each(data, function (i, item) {
    var number = parseInt(item, 10);
    alert(number);
});

DEMO: http://jsfiddle.net/mYysX/

Sign up to request clarification or add additional context in comments.

Comments

2

According to your post you are splitting the string on comma which means you are leaving the quotes around the numbers. This means you are doing the equivalent of parseInt("\"573007\""). You would need to strip the quotes before calling parseInt.

6 Comments

I don't think so.. the one of the purpose of parseInt is to convert it to number. Parses a string argument and returns an integer of the specified radix or base. - Definition from MDN
Yes, it is correct. He is doing the equivalent of parseInt("\"573007\"") because he has split the string on comma without removing quotes. Result is NaN. jsfiddle.net/EFwcx
@apathetic from OP array of strings passed back into a javascript function - I believe he has an array already. It it not a single string.
@Vega from OP: "using the split function with "," as a delimiter"
@apathetic: If it had to be split, it isn't an array -- it's a string. And if it's a string that looks like the stuff above, it's actually JSON and should be parsed -- which would eliminate the quotes anyway.
|

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.