1

I have an array

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

Here every object is coming 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?

5
  • 3
    range.push(parseInt(watch[i])) 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. Commented May 26, 2017 at 14:43
  • 1
    range.push(+watch[i]) Commented May 26, 2017 at 14:44
  • @MrKen that's a neat trick Commented May 26, 2017 at 14:44
  • @Brian , yes , i usually using it :D Commented May 26, 2017 at 14:46

8 Answers 8

6

You can simply convert the value to int.

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

Sign up to request clarification or add additional context in comments.

Comments

3

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);

Comments

2

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]))
}

Comments

1

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]));
}

Comments

1

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

Comments

1

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

var range = watch.map(Number)

4 Comments

That would create a number object, which is not usually what people want.
@JamesKraus "That would create a number object" Not sure what you mean? Number() can accept a string and return a number representation of input string, which is what OP is expecting.
Nevermind, I confused it's behavior with the object constructor. TIL
This should be the answer :)
1

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]));
}

Comments

0

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);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.