0

I have a php page which gets some data in mysql database and return a json file, and then I call this json file with an ajax call :

$.ajax({
        type: "POST",
        dataType: "json",
        url: "get_players_stats.php", //Relative or absolute path to players_stats file
        success: function(data) {
            var list = [];
            $.each(data, function() {
                list.push([this.time, this.playersonline]);
            })
            console.log(list);

        }
    });

the console.log(list); shows me this :

[["-1009843200000", "1"], ["-252460800000", "2"], ["-94694400000", "3"], ["31536000000", "2"],....

but instead I want it to look like this :

[[-1009843200000, 1], [-252460800000, 2], [-94694400000, 3], [31536000000, 2],....

How can I do that ?

2
  • why do you want it to look like that? Javascript is forgiving on variable types, so unless you need it to be an integer (or float), what difference does it make? Commented Jan 8, 2016 at 19:58
  • 1
    This is a duplicate. The issue is on your PHP side. See this answer: stackoverflow.com/questions/1390983/… Commented Jan 8, 2016 at 20:00

4 Answers 4

2

Could you just do something as simple as this?

$.each(data, function() {
    list.push([Number(this.time), Number(this.playersonline)]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Do you really need to convert those values to numeric ones? JavaScript only supports 53 bit integers, so parseInt('10765432100123456789') will be converted to 10765432100123458000 integer.

Working with large integers in JavaScript.

If you still would like to convert the response you can do something like this:

$.each(data, function() {
  list.push([+this.time, +this.playersonline]);
})

3 Comments

good point, this is always a problem in Javascript with BigInts
The times all seem to be a multiple of 1000 (probably they're seconds that have been converted to milliseconds), so the loss of those last few digits of precision may not matter.
@Barmar The problem here that I do not know what the time is.
1

This is a type conversion problem: your data is showing as text format and you want it as number format.

You could parse the text as float in javascript, but that would likely be very inefficient, when instead you could cast the data as the type you need in the SQL query that returns the JSON.

To get you more details on how, it would be good to see how you pull the data into the JSON in the first place.

EDIT:

the actual answer you should be using is here: PHP json_encode encoding numbers as strings

Comments

0

ES6 syntax http://jsbin.com/qamoru/edit?js,console

let arr=[["-1009843200000", "1"], ["-252460800000", "2"], ["-94694400000", "3"], ["31536000000", "2"]];
let newArr = arr.map((item)=> item.map((it)=> Number(it)));
console.log(newArr)

JS Bin on jsbin.com

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.