1

I collect the current value of a range slider every 1 second and store those values in an array. The value are saved as ["34", "23", "21"]. I need them to save as ints like [6, 4, 3, 2] before I can use them as data in my charts.js canvas. Haven't been able to find a clear answer!

2
  • 1
    While storing convert it into int using parseInt() function and store. Commented Dec 15, 2015 at 5:09
  • 1
    jsfiddle.net/g11wm6LL/1 Commented Dec 15, 2015 at 5:11

2 Answers 2

4

Alternatively, you can one-line it using .map().

var arr = ["34", "23", "21"];
arr.map(Number);
>>> [34, 23, 21]
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know you can map to a type. Nice!
1

You can use javscript function parseInt() to acheive it

for example:-

var x=["1","2","3","4"]
x.forEach(function(item){
 console.log(parseInt(x,10));
});

it will console all numeric values

2 Comments

always use parseInt with radix spcified parseInt(x,10) as some old browsers may have unexpected behavior
@Gaafar i agree with you,providing radix would be a plus point as exceptional handling point of view.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.