将数组值更改为字符串中的数字

时间:2022-09-25 08:10:53

I have an array with these values:

我有一个包含这些值的数组:

var items = [Thursday,100,100,100,100,100,100]

I'm grabbing these from the URL query string so they are all string values. I want all columns except the first to be number. The array may vary in the number of columns, so is there a way to set this so items[0] is always a string, but items[n] is always a number?

我从URL查询字符串中抓取这些,因此它们都是字符串值。我希望除第一列之外的所有列都是数字。数组的列数可能不同,所以有没有办法设置这个,所以items [0]总是一个字符串,但items [n]总是一个数字?

3 个解决方案

#1


5  

"...is there a way to set this so items[0] is always a string, but items[n] is always a number?"

“...有没有办法设置这个,所以items [0]总是一个字符串,但items [n]总是一个数字?”

Use .shift() to get the first, .map() to build a new Array of Numbers, then .unshift() to add the first back in.

使用.shift()获取第一个,.map()来构建一个新的Numbers数组,然后使用.unshift()来添加第一个数字。

var first = items.shift();
items = items.map(Number);
items.unshift(first);

DEMO: http://jsfiddle.net/EcuJu/

演示:http://jsfiddle.net/EcuJu/


We can squeeze it down a bit like this:

我们可以把它压缩得像这样:

var first = items.shift();
(items = items.map(Number)).unshift(first);

DEMO: http://jsfiddle.net/EcuJu/1/

演示:http://jsfiddle.net/EcuJu/1/


#2


1  

I think this should work for you. You could set whatever default number you liked instead of 0.

我认为这对你有用。您可以设置您喜欢的默认号码而不是0。

var items = ["Thursday","100","100","100","100","100","100"], i;
for (i = 1; i < items.length; i++)
{
    if(typeof items[i] !== "number")
    {
        items[i] = isNaN(parseInt(items[i], 10)) ? 0 : parseInt(items[i], 10);
    }
}

#3


1  

parseFloat() will convert your string to a number.

parseFloat()会将您的字符串转换为数字。

Here is a sample code for modern browsers (won't work in IE7/IE8):

以下是现代浏览器的示例代码(在IE7 / IE8中不起作用):

var convertedItems=items.map(function(element,index){
  // Convert array elements with index > 0
  return (index>0)?parseFloat(element):element;
});

There's also a parseInt() method for conversion to integers:

还有一个用于转换为整数的parseInt()方法:

parseInt(element,10)

#1


5  

"...is there a way to set this so items[0] is always a string, but items[n] is always a number?"

“...有没有办法设置这个,所以items [0]总是一个字符串,但items [n]总是一个数字?”

Use .shift() to get the first, .map() to build a new Array of Numbers, then .unshift() to add the first back in.

使用.shift()获取第一个,.map()来构建一个新的Numbers数组,然后使用.unshift()来添加第一个数字。

var first = items.shift();
items = items.map(Number);
items.unshift(first);

DEMO: http://jsfiddle.net/EcuJu/

演示:http://jsfiddle.net/EcuJu/


We can squeeze it down a bit like this:

我们可以把它压缩得像这样:

var first = items.shift();
(items = items.map(Number)).unshift(first);

DEMO: http://jsfiddle.net/EcuJu/1/

演示:http://jsfiddle.net/EcuJu/1/


#2


1  

I think this should work for you. You could set whatever default number you liked instead of 0.

我认为这对你有用。您可以设置您喜欢的默认号码而不是0。

var items = ["Thursday","100","100","100","100","100","100"], i;
for (i = 1; i < items.length; i++)
{
    if(typeof items[i] !== "number")
    {
        items[i] = isNaN(parseInt(items[i], 10)) ? 0 : parseInt(items[i], 10);
    }
}

#3


1  

parseFloat() will convert your string to a number.

parseFloat()会将您的字符串转换为数字。

Here is a sample code for modern browsers (won't work in IE7/IE8):

以下是现代浏览器的示例代码(在IE7 / IE8中不起作用):

var convertedItems=items.map(function(element,index){
  // Convert array elements with index > 0
  return (index>0)?parseFloat(element):element;
});

There's also a parseInt() method for conversion to integers:

还有一个用于转换为整数的parseInt()方法:

parseInt(element,10)