最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove single quotes from array object - Stack Overflow

programmeradmin2浏览0评论

I have an array

watch = ['52055690','52056600','52056603','52055260','52055255','52055276']

Here every object is ing with single quotes. I just need to get array in following form

[52055690, 52056600,52056603,52055260,52055255,52055276]

I tried following

var range = [];
for (var i = 0; i < watch.length; i ++ ) {
  range.push( watch[i])
}

But still i was not able to make it out. Could anyone please help me out with it?

I have an array

watch = ['52055690','52056600','52056603','52055260','52055255','52055276']

Here every object is ing with single quotes. I just need to get array in following form

[52055690, 52056600,52056603,52055260,52055255,52055276]

I tried following

var range = [];
for (var i = 0; i < watch.length; i ++ ) {
  range.push( watch[i])
}

But still i was not able to make it out. Could anyone please help me out with it?

Share Improve this question edited May 26, 2017 at 14:42 j08691 208k32 gold badges269 silver badges280 bronze badges asked May 26, 2017 at 14:40 animeshanimesh 1512 gold badges3 silver badges12 bronze badges 5
  • 3 range.push(parseInt(watch[i])) – Brian Commented May 26, 2017 at 14:42
  • What @Brian said. Conceptually, you aren't trying to "remove single quotes", you're trying to cast or parse strings to integers. – Adrian Commented May 26, 2017 at 14:43
  • 1 range.push(+watch[i]) – Canh Nguyen Commented May 26, 2017 at 14:44
  • @MrKen that's a neat trick – Brian Commented May 26, 2017 at 14:44
  • @Brian , yes , i usually using it :D – Canh Nguyen Commented May 26, 2017 at 14:46
Add a ment  | 

8 Answers 8

Reset to default 6

You can simply convert the value to int.

range.push(parseInt(watch[i]));

Use map() with Number() and it will return a new Array with numbers.

var watch = ['52055690','52056600','52056603','52055260','52055255','52055276']
var result = watch.map(Number)

console.log(result);

So I think you are trying to convert strings to integers. You can do this with the parseInt function. You can implement this to iterate over your array and convert each string like this:

var range = [];
for (var i = 0; i < watch.length; i ++ ) {
    range.push( parseInt(watch[i]))
}

You could just parse the value to int or to float using the parseInt() or parseFloat() functions.

You could try something like this:

var range = [];
for (var i = 0; i < watch.length; i ++ ) {
  range.push( parseInt(watch[i]));
}

You can use parseInt to convert string to number.

watch = ['52055690','52056600','52056603','52055260','52055255','52055276' ]
var range = [];
for (var i = 0; i < watch.length; i ++ ) {
range.push( parseInt(watch[i]))
}
console.log(range)

Thanks you

You can use .map(), Number() constructor

var range = watch.map(Number)

Use parseInt to convert to number. Number could be used as well. Below is the code.

for (var i = 0; i < watch.length; i ++) { 
  range.push(parseInt(watch[i]));
}

cast to integer.

var result = watch.map(parseInt);

var watch = ['52055690','52056600','52056603','52055260','52055255','52055276'];
var result = watch.map(function(e) { return parseInt(e); });
console.log(result);

发布评论

评论列表(0)

  1. 暂无评论