[Solved] How to get unique value of json and sum another value in array?


You need to check existence of value in array using .indexOf(). If value doesn’t exist in array, insert it using .push(). About WinProbability, if exist, increase value of it.

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

13

solved How to get unique value of json and sum another value in array?