如何在 Protovis (Javascript) 中访问类似字典的结构
我正在尝试使用 protovis 可视化 flickr 数据集。我确实理解可视化部分,但我对访问数据有疑问。我获得了一个示例可视化,它访问数据如下:
var data = pv.range(250).map(function(row) {
return {
views: parseInt(Data.data(row, 2)), //refers to the 4 row and 2nd collumn in CSV
users: Data.data(row, 6),
date: Data.data(row, 8))), //more collumns excist but for now we only use these
};
});
据我了解,数据集的一部分现在存储在变量数据中,即视图、用户和日期。这个变量可以像字典一样被访问吗?
我想做的是检查一个用户是否出现超过 2 次的日期。我想按如下方式循环遍历 var 数据:
dateUserDict {};
for (d=0; d < data.date.length; d++ ){
for (i=0; i < data.users.length; i++ ){
for (j=0; j < data.users.length; j++){
if (data.users[i] == data.users[j]){
userCounter++ //this should count the number of occurences of a specific user on a specific date
dateUserDict[data.date] = [data.user][userCounter]}
}
}
}
这似乎不起作用。我试图将事件(用户在特定日期发生的次数)存储在字典中。如果我得到所描述的字典,我可以轻松地想象整个事情。但正是这种从第一个字典(数据)到第二个字典(dateUserDict)的转换让我烦恼!
非常感谢任何帮助或推动!
谢谢乔里特
I am trying to visualize a flickr dataset using protovis. I do understand the visualization part, but i have a question about accessing the data however. I was provided an example visualization and it accesses the data as folllowing:
var data = pv.range(250).map(function(row) {
return {
views: parseInt(Data.data(row, 2)), //refers to the 4 row and 2nd collumn in CSV
users: Data.data(row, 6),
date: Data.data(row, 8))), //more collumns excist but for now we only use these
};
});
As i understand a part of the data set is now stored in the variable data, namely views, users and date. Is this variable able to beaccessed like a dictionary?
What i am trying to do is checking whether there are date on which one user occurs more than 2 times. I thought of looping through the var data as follows:
dateUserDict {};
for (d=0; d < data.date.length; d++ ){
for (i=0; i < data.users.length; i++ ){
for (j=0; j < data.users.length; j++){
if (data.users[i] == data.users[j]){
userCounter++ //this should count the number of occurences of a specific user on a specific date
dateUserDict[data.date] = [data.user][userCounter]}
}
}
}
This does not seem to work. I am trying to store the events (the number of times a user occurs on a specific date) in a dictionary. If i get the dictionary as described i can easily visualise the whole thing. But it is this conversion from the first dict (data) to the second (dateUserDict) which bugs me!
Any help or a push is highly appreciated!
Thanks
jorrit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的函数将生成一个 Javascript 对象数组。
结果将如下所示:
因此,不要使用 data.users.length,而应使用 data.length,并且不应使用 data.users[i],而应使用 data[i].users 等。
The function you provided will product a Javascript array of objects.
The result will look something like this:
So instead of using data.users.length, use data.length, and instead of data.users[i], you should be using data[i].users, etc.