如何在 Protovis (Javascript) 中访问类似字典的结构

发布于 2024-11-02 01:53:50 字数 1090 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轮廓§ 2024-11-09 01:53:50

您提供的函数将生成一个 Javascript 对象数组。

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
  };
});

结果将如下所示:

var data = [ {views:10, users: 9, date: '09/13/1975'}, ... ]

因此,不要使用 data.users.length,而应使用 data.length,并且不应使用 data.users[i],而应使用 data[i].users 等。

The function you provided will product a Javascript array of objects.

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
  };
});

The result will look something like this:

var data = [ {views:10, users: 9, date: '09/13/1975'}, ... ]

So instead of using data.users.length, use data.length, and instead of data.users[i], you should be using data[i].users, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文