1

I have a csv file converted to a jQuery object using jQuery CSV (https://github.com/evanplaice/jquery-csv).

Here is the code for that:

    $.ajax({
        type: "GET",
        url: "/path/myfile.csv",
        dataType: "text",
        success: function(data) {
        // once loaded, parse the file and split out into data objects
        // we are using jQuery CSV to do this (https://code.google.com/p/jquery-csv/)

        var data = $.csv.toObjects(data);
    });

I'm adding up the bushels_per_day values by company and want to refactor my code to make it more compact.

Using this answer: Sum values in jQuery object by key, I am able to loop through using $.each();

The object format is like so:

    var data = [
        "0":{
            bushels_per_day: "145",
            plant_city: "Decatur",
            plant_company: "AGP",
        },
        "1":{
            bushels_per_day: "125",
            plant_city: "Cedar Rapids",
            plant_company: "AGP",
        },
        "2":{
            bushels_per_day: "345",
            plant_city: "Ralston",
            plant_company: "AGP",
        },
        "3":{
            bushels_per_day: "55",
            plant_city: "Dawson",
            plant_company: "ADM",
        },
        "4":{
            bushels_per_day: "55",
            plant_city: "Dawson",
            plant_company: "ADM",
        },
        // ... more objects
    ]

And here is the $.each() loop:

    var sumADM = 0;
    var sumAGP = 0;
    // var for each company



    $.each(data, function (index, value) {
        var capacity = parseInt(value.bushels_per_day, 10);
        var company = value.plant_company.replace(/\W+/g, '_').toLowerCase();


        if (company == 'adm') {
            sumADM += capacity;
        }
        if (company == 'agp') {
            sumAGP += capacity;
        }
        // ... and so on for each company
    });

    console.log(sumADM, sumAGP); // and so on.

This works, but how can I refactor this so that I don't need a sum variable and if statement for each company? Currently the sum variable and console.log() must be outside the loop in order to return the correct totals.

Is there a better, more compact way to do this?

4

1 回答 1

3

您可以将总和作为属性放在对象上:

var sums = {
    ADM: 0,
    AGP: 0
};

$.each(data, function (index, value) {
    var capacity = parseInt(value.bushels_per_day, 10);
    var company = value.plant_company.replace(/\W+/g, '_').toUpperCase(); // Note change here

    sums[company] += capacity;
});

console.log(sums.ADM, sums.AGP); // and so on.

或者循环输出它们:

Object.keys(sums).forEach(function(company) {
    console.log(sums[company]);
});

如果公司不同,你甚至可以做惰性初始化:

var sums = {};

$.each(data, function (index, value) {
    var capacity = parseInt(value.bushels_per_day, 10);
    var company = value.plant_company.replace(/\W+/g, '_').toUpperCase();

    sums[company] = (sums[company] || 0) + capacity;
});

Object.keys(sums).forEach(function(company) {
    console.log(sums[company]);
});

sums[company] = (sums[company] || 0) + capacity;条线的运作方式是,如果我们以前没有见过那家公司,sums[company]将会是undefined。由于undefined是错误的,JavaScript奇怪的强大||运算符将使用右手操作数的值 ( 0) 作为其结果。如果我们以前见过companyand sums[company]is ,这也是正确的0,但没关系, a0是 a 0。所有其他值(1等等)都是真实的,所以sum[company] || 0也会1如此(左侧操作数的值)。


旁注:注意我使用的是公司字符串toUpperCase而不是toLowerCase公司字符串,因此它们与属性匹配。

于 2015-01-29T14:02:43.843 回答