34

I have created an array:

var endFlowArray = new Array;
for (var endIndex in flowEnd) { // <- this is just some numbers 
    for (var i in dateflow) { // <- same thing 
        var check = $.inArray(flowEnd[endIndex], dateflow[i]);
        if (check >= 0) {
            endFlowArray.push(i);
            flowEnd[endIndex] = null;
        }
    }
}

How can I convert a string array of:

["286", "712", "1058"]

to integer array like:

[286, 712, 1058]
5
  • 8
    It's an easy fix. .push(+i) or .push(parseInt(i)). Simply converting a string to an integer. Commented May 10, 2012 at 20:39
  • 1
    my endFlowArray came up with something like this You need to explain this statement. How did it "come up"? Are you using a javascript debugger? alert? Something else? Commented May 10, 2012 at 20:40
  • is dateflow an array of strings or numbers? Commented May 10, 2012 at 20:41
  • Kevin B solve it BUT i still need to know whar happend Commented May 10, 2012 at 20:43
  • Show the creation of the strings in dataflow and you have your answer Commented May 10, 2012 at 20:44

4 Answers 4

172
var arrayOfNumbers = arrayOfStrings.map(Number);
Sign up to request clarification or add additional context in comments.

1 Comment

Short and sweet way to typecast.
11

Strings in the console are symbolized by wrapping them in quotes. By that fact, we can assume that i is a string. Convert it to an integer and it will no longer be a string and no longer have those quotes.

endFlowArray.push(+i);

Your "numbers" in flowEnd and dateFlow are actually strings, not numbers.

Comments

4

To convert entire array's data type we can use map():

let numberArray = stringArray.map(Number)

Comments

1

try this:

let numberArray = stringArray.map(el=>parseInt(el))

1 Comment

That gets the job done, but some formatting wouldn't hurt! :)

Your Answer

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