[英]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. 这个问题的背景是我正在尝试从谷歌可视化日历中的addListener访问所选单元格。 I could print the selected cell using
JSON.stringify(chart.getSelection());
我可以使用
JSON.stringify(chart.getSelection());
打印选定的单元格JSON.stringify(chart.getSelection());
. 。 It prints as
[{"date":1468195200000,"row":0}]
. 它打印为
[{"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
. 但它显示为
Undefined
。 When I print the obj
it displays as [object, object] 当我打印
obj
它显示为[对象,对象]
If you need more information please let me know. 如果您需要更多信息,请告诉我。 Any suggestion would be appreciated.
任何建议将不胜感激。
JSON.stringify() serializes a data object into a string. JSON.stringify()将数据对象序列化为字符串。 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)
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);
careful when accessing the first array element directly when listening for the 'select'
event 在收听
'select'
事件时直接访问第一个数组元素时要小心
chart.getSelection()[0]
the 'select'
event is fired, both when a selection is made, AND removed 在进行选择和删除时,都会触发
'select'
事件
so the above line will fail when the selection is removed 因此删除选择时上面的行将失败
check the length
before accessing... 在访问之前检查
length
...
selection = chart.getSelection();
// check selection length
if (selection.length > 0) {
console.log(selection[0].date);
} else {
console.log('selection removed');
}
see following working snippet, select a date, then select again to remove the selection... 请参阅以下工作代码段,选择日期,然后再次选择以删除选择...
google.charts.load('current', { callback: function () { var dataTable = new google.visualization.DataTable(); dataTable.addColumn({ type: 'date', id: 'Date' }); dataTable.addColumn({ type: 'number', id :'Score' }); dataTable.addRows([ [ new Date(2016, 3, 13), 101 ], [ new Date(2016, 3, 14), 102 ], [ new Date(2016, 3, 15), 103 ], [ new Date(2016, 3, 16), 104 ], [ new Date(2016, 3, 17), 105 ] ]); var chart = new google.visualization.Calendar(document.getElementById('chart_div')); google.visualization.events.addListener(chart, 'select', function () { var selection = chart.getSelection(); // check selection length if (selection.length > 0) { console.log(selection[0].date); } else { console.log('selection removed'); } }); chart.draw(dataTable); }, packages:["calendar"] });
<script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>
This is an array of objects however there is only one object in this array (or so I think) so what you should do is console.log(obj[0].date);
这是一个对象数组,但是这个数组中只有一个对象(或者我认为),所以你应该做的是
console.log(obj[0].date);
instead! 代替!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.