简体   繁体   English

如何将JavaScript对象传递给另一个函数?

[英]How to pass JavaScript object to another function?

I am downloading csv file from AWS S3 bucket so that I can use it in D3. 我正在从AWS S3存储桶下载csv文件,以便可以在D3中使用它。

No problem downloading the csv file. 下载csv文件没有问题。 In code below the console.log(data.Body.toString()) prints out the csv file contents in expected format console.log(data.Body.toString())下面的代码中,以预期格式打印出csv文件内容

However, I am not sure how to pass the downloaded csv file content to D3 in code below. 但是,我不确定如何在下面的代码中将下载的csv文件内容传递给D3。

The file contents are already in nice csv file format. 文件内容已经是不错的csv文件格式。

Question: how to replace merged.csv below with the mergedcsv object? 问:如何更换merged.csv与下面mergedcsv对象?

<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>

updated to add sample of mergedcsv content eg output of console.log(mergedcsv); 更新以添加示例mergedcsv内容,例如console.log(mergedcsv);输出console.log(mergedcsv); or console.log(data.Body.toString()); 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

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. 阅读d3.csv上的文档后,它会从URL加载CSV文件,但是在您的示例中,您已经加载了数据,只需解析即可。 So d3.csv isn't going to be of any use. 因此d3.csv不会有任何用处。

It looks like the D3 function for parsing is d3.csv.parse(data) which will return an array of parsed data. 似乎用于解析的D3函数是d3.csv.parse(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
        });
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个函数的另一个对象的属性传递给一个函数,并在javascript中调用它? - How to pass a function that's a property of another object into a function and call it in javascript? Javascript通过另一个函数将对象传递给函数 - Javascript pass object into function by another function 如何将对象的方法作为参数传递给Javascript中的另一个函数 - How to pass an object's method as a parameter to another function in Javascript JavaScript:如何将对象值从一个函数传递给另一个函数 - JavaScript: how to pass object value from one function to another Javascript | 将对象函数作为参数传递给另一个对象 - Javascript | Pass an objects function as argument into another object 如何在JavaScript中将对象传递给函数 - How to pass object into function in JavaScript 如何将对象值传递到另一个对象JavaScript - How to pass an object value into another object JavaScript 在Javascript函数中设置JSON对象并将其传递给另一个函数 - Set a JSON object in a Javascript function and pass it to another function 如何将数组传递给Javascript中的另一个函数? - How to pass an array to another function in Javascript? 如何将值传递给Javascript中的另一个函数? - How to pass value to another function in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM