多个阵列,合并为一个,获得平均值d3

时间:2014-06-17 19:06:10

标签: javascript arrays json d3.js

我有一个循环遍历json对象并将它们推入数组的函数。以下是其中一个对象的示例。每个对象将具有完全相同的统计数据键 - 但当然,不同的值。我需要循环遍历所有数组并将统计数据相加,然后将它们除以数组的数量。当然,我可以通过几个循环函数来做到这一点......但是因为我正在使用d3,我想我会尝试找出内置的内容,即d3。意思()。有没有办法获取所有数组中的所有值并在d3中平均它们?

{
        "name": "Mr Man",
        "position": "G",
        "image": "img/mrman.jpg",
        "stats": {
            "GP": 35,
            "Min": 567,
            "Points": 191,
            "Rebounds": 46,
            "Assists": 72,
            "Steals": 18,
            "Blocks": 1,
            "Turnovers": 37,
            "FGAtt": 151,
            "FGMade": 63,
            "TPtAtt": 55,
            "TPMade": 18,
            "FTAtt": 47,
            "FTMade": 71
        }
        // doing this works but it's very inefficient...I should be able to loop this
        var tempdata = playerArr.slice()
    console.log(tempdata + "tempdata")


    function meanVal(value) {
        return d3.mean(tempdata, function(d) {
        return d.stats[value] })
    }
    var avgArray = {};
    avgArray.GP = meanVal('GP');
    avgArray.Min = meanVal('Min');
    avgArray.FGMade = meanVal('FGMade');
    avgArray.FGAtt = meanVal('FGAtt');
    avgArray.TPMade = meanVal('TPMade');
    avgArray.TPAtt = meanVal('TPAtt');
    avgArray.Points = meanVal('Points');
    avgArray.Rebounds = meanVal('Rebounds');
    avgArray.Assists = meanVal('Assists');
    avgArray.Turnovers = meanVal('Turnovers');
    avgArray.Steals = meanVal('Steals');
    avgArray.Blocks = meanVal('Blocks');

    console.log(avgArray.Min + "ar")

这样的东西不起作用......也许是因为我专注于多个阵列而不是一个阵列:

function returnMean(value) {
        return d3.mean(playerArr.stats, function(d) {
        return d [value] })
    }

    var myMean = returnMean('Points');
    console.log(myMean)
            console.log(playerArr.length) // returns more than 1, say 2 or 3 or 4

在这种情况下,我可能不得不使用拼接:         var tempdata = playerArr.slice()         function returnMean(value){             return d3.mean(tempdata,function(d){             return d.stats [value]})         }

0 个答案:

没有答案