Remove quotes from values ​​in a JavaScript Array object

I am trying to format the encoded JSON array correctly. Here is my PHP MySQL Query:

$query = "SELECT date, value1, value2
            FROM testdata
            ORDER BY date ASC";  


         $result = mysql_query($query);
         $myChartData = array();
          while($row = mysql_fetch_assoc($result)){
            $myChartData[] = $row;
                        }

   <script type="text/javascript">
    var chartData = <?php echo json_encode($myChartData); ?>;

On the console, the object is as follows:

[Object { date="2011-02-23", value1="133034", value2="12105.78"},
 Object { date="2011-02-24", value1="122290", value2="12068.50"},
 Object { date="2011-03-08", value1="453142", value2="12214.38"}]

The quotes around the date remain, but those that are on value1 and value2 must be removed.

Noob tutorial in all this ... Thanks for your help!

+3
source share
2 answers

Simple javascript solution:

function formatChartData(data) {
    for(var i = 0, length = data.length; i < length; i++) {
        data[i].value1 = parseInt(data[i].value1, 10);
        data[i].value2 = parseFloat(data[i].value2);
    }
    return data;
}

var chartData = formatChartData(<?php echo json_encode($myChartData); ?>);
+1
source

Firstly:

The quotes around the date remain, but those that are on value1 and value2 must be removed.

! , , chartData. , , .
( , ).


, . , .

- . , .


- ( ),

JavaScript, [MDN].

+2

All Articles