Javascript 将字符串数组转换为整数数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10541770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:47:18  来源:igfitidea点击:

convert string array to integer array

javascriptjquery

提问by Jonas Anseeuw

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]

采纳答案by Kevin B

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

控制台中的字符串通过用引号括起来来表示。根据这个事实,我们可以假设这i是一个字符串。将其转换为整数,它将不再是字符串,也不再有那些引号。

endFlowArray.push(+i);

Your "numbers" in flowEndand dateFloware actually strings, not numbers.

您的“数字”中flowEnddateFlow实际上是字符串,而不是数字。

回答by Jonas Anseeuw

var arrayOfNumbers = arrayOfStrings.map(Number);

回答by Gautam Mukherjee

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

要转换整个数组的数据类型,我们可以使用map()

let numberArray = stringArray.map(Number)

回答by khaledsimplon

try this:

尝试这个:

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