下图是数组中2个对象的屏幕截图。
如何在此数组中检索日期和字符串“”?请考虑数组中是否有多个对象。
到目前为止我已经创建了这个功能:
function printData(data) {
$.each(data, function(i, item) {
//console.log(item);
$.each(item, function(j, c) {
//console.log(c);
$.each(c, function(k, l){
//console.log(l.v);
});
});
});
//console.log(data.c.v);
console.dir(data);
}
你能不能调整这个功能来获得我想要的东西。
编辑:
对象:
data = new google.visualization.DataTable();
data.addColumn('datetime', 'start');
data.addColumn('datetime', 'end');
data.addColumn('string', 'content');
侦听器允许用户将事件添加到时间轴,然后将数据添加到时间轴,如下所示:
var options = {
"width": "100%",
"height": "300px",
"editable": true,
"style": "box"
};
timeline.draw(data, options);
这是删除功能(可能有助于区分对象的选择):
function doDelete() { /*** Delete the currently selected event */
// retrieve the selected row
var sel = timeline.getSelection();
if (sel.length) {
if (sel[0].row != undefined) {
var row = sel[0].row;
}
}
if (row != undefined) {
timeline.deleteItem(row);
} else {
alert("First select an event, then press remove again");
}
}
console.log(JSON.stringify(data, null, 4));
输出:
{
"cols": [
{
"id": "",
"label": "start",
"pattern": "",
"type": "datetime"
},
{
"id": "",
"label": "end",
"pattern": "",
"type": "datetime"
},
{
"id": "",
"label": "content",
"pattern": "",
"type": "string"
}
],
"rows": [
{
"c": [
{
"v": "Date(2014, 3, 24)",
"f": null
},
{
"v": "Date(2014, 4, 1)",
"f": null
},
{
"v": "Subgoal A",
"f": null
}
]
},
{
"c": [
{
"v": "Date(2014, 4, 1)",
"f": null
},
{
"v": "Date(2014, 4, 8)",
"f": null
},
{
"v": "Subgoal B",
"f": null
}
]
}
],
"p": null
}
答案 0 :(得分:1)
您实际上似乎有4个级别,并且仅在屏幕截图中显示了.rows
对象的data
数组。但是,您不应该只围绕它包装另一个循环,而是显式访问该属性 - 就像最后使用.v
一样。此外,.c
属性应该明确地被访问,而不是仅仅被枚举。
function printData(data) {
console.log(JSON.stringify(data)); // assuming this is the shown output
$.each(data.rows, function(i, row) {
$.each(row.c, function(j, item) {
console.log(item.v);
});
});
}
答案 1 :(得分:0)
也许这适合你:
$(arr).each(function (index) {
$(arr[index]).each(function (index1) {
$(arr[index][index1]).each(function (index2) {
alert(arr[index][index1][index2]);
});
});
});
答案 2 :(得分:0)
请改为尝试:
//obj - your object
$.each(obj.rows, function(i,rows){
$.each(rows, function(j, c){
$.each(c, function(k, data){
console.log("f: " + data.f + "v: " + data.v);
});
});
});