dhanashri dhanashri - 4 months ago 17
JSON Question

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

I have one json value in that json file three movie's name. JSON data are dynamic data. I want to calculate percentage on

WinProbability
field. I want output as follows

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


My JSON file shown

[
{
"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"
}
]


My jquery code

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

});
});


Please help me how to do this. I tried make unique name of nominee but I don't know to add
WinProbability
and calculate percentage.

Answer

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