解析数组中的每个元素为一个整数 [英] Parsing every element of an array as an integer

查看:146
本文介绍了解析数组中的每个元素为一个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我需要分成数组,然后数组中的每个元素执行数学函数。

I have a string which I need to split into an array and then perform mathematical functions on each element of the array.

目前我做这样的事情。 (其实,我做这样的事,但是这是一个很简单的例子来说明我的问题!

Currently I am doing something like this. (Actually, I am doing nothing like this, but this is a very simple example to explain my question!

var stringBits = theString.split('/');

var result = parseInt(stringBits[0]) + parseInt(stringBits[3]) / parseInt(stringBits[1]);

我想知道的是,如果有一种方法,我可以在一个阵列中的每个元素转换成某种类型,将不必每次都明确地分析它阻止我什么。

What I would like to know is if there is a way I can convert every element of an array into a certain type that would stop me from having to explicitly parse it each time.

推荐答案

你可以通过它循环:

for(var i = 0; i < stringBits.length; i++) {

    stringBits[i] = parseInt(stringBits[i]);
}

这篇关于解析数组中的每个元素为一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆