-2

this is my code

var googleChartObj = [];

var xValues = ['3', '4', '9', '10']

if (xValues.length > 20) {
    xValues = xValues.slice(-20);
} else {
    xValues = xValues;
}

for (var i = 0; i < xValues.length; i++) {
    googleChartObj.push([i, xValues[i]]);
}

console.log("googleChartObj",  googleChartObj);

and this is the result

googleChartObj [ [ 0, '3' ], [ 1, '4' ], [ 2, '9' ], [ 3, '10' ] ]

but I want to convert like that

googleChartObj [ [ 0, 3 ], [ 1, 4 ], [ 2, 9 ], [ 3, 10 ] ]

I'M LEEBAN
  • 11
  • 4
  • 1
    There are dozens of questions already on the site about how to convert a string to a number. [Here's my answer to one of them](https://stackoverflow.com/questions/28994839/why-does-string-to-number-comparison-work-in-javascript/28994875#28994875). – T.J. Crowder Apr 07 '22 at 10:56
  • 1
    Welcome to Stack Overflow! Please take the [tour] if you haven't already (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Please search before posting. More about searching [here](/help/searching). – T.J. Crowder Apr 07 '22 at 10:56
  • Thank you @T.J.Crowder I know everyone's time is important here, from now I'll research before posting questions :) – I'M LEEBAN Apr 08 '22 at 05:13

1 Answers1

-1

To convert a string into a number there is parseInt function in javascript.

So all you need to do is while pushing the index and value convert the value to integer

googleChartObj.push([i, parseInt(xValues[i])]);

Here's the full code:

var googleChartObj = [];

var xValues = ['3', '4', '9', '10']

if (xValues.length > 20) {
    xValues = xValues.slice(-20);
} else {
    xValues = xValues;
}

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

    googleChartObj.push([i, parseInt(xValues[i])]);
}

console.log("googleChartObj",  googleChartObj);