Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to access data in JSON.stringify object

The background for this question is I'm trying to access the selected cell from addListener in google visualization calendar. I could print the selected cell using JSON.stringify(chart.getSelection());. It prints as [{"date":1468195200000,"row":0}].

My problem is to access this date object. I tried to access it as

var obj=JSON.stringify(chart.getSelection());
        console.log('selected value '+obj.date);

But it displays as Undefined. When I print the obj it displays as [object, object]

If you need more information please let me know. Any suggestion would be appreciated.

like image 287
Zusee Weekin Avatar asked Jul 12 '16 02:07

Zusee Weekin


2 Answers

JSON.stringify() serializes a data object into a string. Just access the object directly.

var data = chart.getSelection()[0]; // assuming there is only ever one item in the selection array.
console.log(data.date)
like image 117
chardy Avatar answered Oct 13 '22 08:10

chardy


Try this:

var data = [{"date":1468195200000,"row":0}]

var rw = data[0].row;
var dt = new Date(data[0].date);

console.log(rw);
console.log(dt);
like image 42
Fadhly Permata Avatar answered Oct 13 '22 08:10

Fadhly Permata