如何获取json的唯一值并在数组中求和另一个值?

时间:2016-07-24 15:04:34

标签: javascript jquery arrays json object

我在json文件中有一个json值,三个电影的名字。 JSON数据是动态数据。我想计算WinProbability字段的百分比。我想要输出如下

movie_name = [ "12 Years a Slave", "American Hustle", "Captain"]
percentage = [ 3.47, 2.54, 4.22]

我的JSON文件显示

[
    {
        "ID":1,"Nominee":"12 Years a Slave","WinProbability":0.00,"WinType":"Win"
    },
    {
        "ID":2,"Nominee":"12 Years a Slave","WinProbability":2.81,"WinType":"Win"
    },
    {   
        "ID":3,"Nominee":"12 Years a Slave","WinProbability":0.66,"WinType":"Nominated"
    },
    {   
        "ID":1,"Nominee":"American Hustle","WinProbability":1.62,"WinType":"Nominated"
    },
    {
        "ID":2,"Nominee":"American Hustle","WinProbability":0.85,"WinType":"Win"
    },
    {
        "ID":3,"Nominee":"American Hustle","WinProbability":0.07,"WinType":"Win"
    },
    {   
        "ID":1,"Nominee":"Captain Phillips","WinProbability":2.70,"WinType":"Nominated"
    },
    {
        "ID":2,"Nominee":"Captain Phillips","WinProbability":0.00,"WinType":"Win"
    },
    {   
        "ID":3,"Nominee":"Captain Phillips","WinProbability":1.52,"WinType":"Win"
    }
]

我的jquery代码

$(function() {  
    $.getJSON('movie.json', function(data) {                

                var movie_name = [],
                    percentage = [];

                data.forEach(function(obj) {
                    if (movie_name.indexOf(obj.Nominee) == -1)
                        movie_name.push(obj.Nominee);

                    var lastIndex = movie_name.length - 1;       
                    if (typeof percentage[lastIndex] == "undefined")
                        percentage.push(obj.WinProbability);
                    else
                        percentage[lastIndex] += obj.WinProbability; 
                });

                console.log(movie_name);
                console.log(percentage);

            });
});

请帮我解决这个问题。我尝试使用被提名者的唯一名称,但我不知道要添加WinProbability并计算百分比。

可以将数据更改为以下格式

    {
    "Win": [
        {
            "0": 3.47,
            "name": "12 Years a Slave"
        },{
            "0": 2.54,
            "name": "American Hustle"
        },{
            "0": 4.22,
            "name": "Captain Phillips"
        }
    ],
    "Nominated":[
        {
            "0": 1.2,
            "name": "12 Years a Slave"
        },{
            "0": 1,
            "name": "American Hustle"
        },{
            "0": 1,
            "name": "Captain Phillips"
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

您需要使用.indexOf()检查数组中是否存在值。如果数组中不存在值,请使用.push()插入。关于WinProbability,如果存在,则增加它的值。

 var json = [
    {"ID":1,"Nominee":"12 Years a Slave","WinProbability":0.00,"WinType":"Win"},
    {"ID":2,"Nominee":"12 Years a Slave","WinProbability":2.81,"WinType":"Win"},
    {"ID":3,"Nominee":"12 Years a Slave","WinProbability":0.66,"WinType":"Nominated"},
    {"ID":1,"Nominee":"American Hustle","WinProbability":1.62,"WinType":"Nominated"},
    {"ID":2,"Nominee":"American Hustle","WinProbability":0.85,"WinType":"Win"},
    {"ID":3,"Nominee":"American Hustle","WinProbability":0.07,"WinType":"Win"},
    {"ID":1,"Nominee":"Captain Phillips","WinProbability":2.70,"WinType":"Nominated"},
    {"ID":2,"Nominee":"Captain Phillips","WinProbability":0.00,"WinType":"Win"},
    {"ID":3,"Nominee":"Captain Phillips","WinProbability":1.52,"WinType":"Win"}
];

var movie_name = [],
    percentage = [];
  
json.forEach(function(obj) {
    if (movie_name.indexOf(obj.Nominee) == -1)
        movie_name.push(obj.Nominee);
    
    var lastIndex = movie_name.length - 1;       
    if (typeof percentage[lastIndex] == "undefined")
        percentage.push(obj.WinProbability);
    else
        percentage[lastIndex] += obj.WinProbability; 
});

console.log(movie_name);
console.log(percentage);