如何将JavaScript对象传递给另一个函数? [英] How to pass JavaScript object to another function?

查看:303
本文介绍了如何将JavaScript对象传递给另一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从AWS S3存储桶下载csv文件,以便可以在D3中使用它.

I am downloading csv file from AWS S3 bucket so that I can use it in D3.

下载csv文件没有问题.在console.log(data.Body.toString())下面的代码中,以预期格式打印出csv文件内容

No problem downloading the csv file. In code below the console.log(data.Body.toString()) prints out the csv file contents in expected format

但是,我不确定如何通过下面的代码将下载的csv文件内容传递给D3.

However, I am not sure how to pass the downloaded csv file content to D3 in code below.

文件内容已经是不错的csv文件格式.

The file contents are already in nice csv file format.

问题:如何用mergedcsv对象替换下面的merged.csv?

Question: how to replace merged.csv below with the mergedcsv object?

<script type="text/javascript">

var bucket = new AWS.S3();

bucket.getObject({
    Bucket: 's3-google-analytics', 
    Key: 'merged.csv'
}, 
    function awsDataFile(error, data) {
        if (error) {
            return console.log(error);
        }
        mergedcsv = data.Body.toString();
        console.log(data.Body.toString());
    }
);

// how to replace the "merged.csv" below with the mergedcsv object above?

d3.csv("merged.csv", function(data) {
    var parseDate = d3.time.format("%Y%m%d").parse;
    var counter = 0;
    data.forEach(function(d) {
        etc
    });
});

</script>

已更新以添加mergedcsv内容的样本,例如console.log(mergedcsv);console.log(data.Body.toString());的输出:

updated to add sample of mergedcsv content eg output of console.log(mergedcsv); or console.log(data.Body.toString()); :

yyyymm,date,hostname,source,PPCC,in30days,sessionDuration,users,sessions,newUsers,twitterSessions,bounceRate,sessionsPerUser
201203,20120330,journal,google,PPCC,>30days,26.25,4,4,4,0,75.0,1.0
201203,20120331,journal,(direct),PPCC,>30days,0.0,3,3,3,0,100.0,1.0
201203,20120331,journal,bing,PPCC,>30days,0.0,1,1,1,0,100.0,1.0

推荐答案

阅读d3.csv上的文档后,它会从URL加载CSV文件,但是在您的示例中,您已经加载了数据,只需要解析它.因此d3.csv不会有任何用处.

After reading a the docs on d3.csv, it loads a CSV file from a URL, but in your example, you've already loaded the data, you just need to parse it. So d3.csv isn't going to be of any use.

看起来,用于解析的D3函数是d3.csv.parse(data),它将返回已解析数据的数组. (文档位于此处)

It looks like the D3 function for parsing is d3.csv.parse(data) which will return an array of parsed data. (Docs are here)

所以您可以做类似...

So you could do something like…

var bucket = new AWS.S3();
var mergedcsv;

bucket.getObject({
    Bucket: 's3-google-analytics', 
    Key: 'merged.csv'
}, 
    function awsDataFile(error, data) {
        if (error) {
            return console.log(error);
        }
        mergedcsv = d3.csv.parse(data.Body.toString()); 

        // now mergedcsv will be an array, so you can do whatever you were going to do with it before, i.e...
        var parseDate = d3.time.format("%Y%m%d").parse;
        var counter = 0;
        data.forEach(function(d) {
           etc
        });
    }
);

这篇关于如何将JavaScript对象传递给另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆