How do I calculate the "positives" and "negatives" functions from a json file to HTML in percentage?

1

Save, I have a JSON file with "positives" and "negatives" variables, I'd like to turn those values into percent, "positives" being "Like" and "negatives" being "Do not like." After that how could I organize the profiles based on this data?

JSON file:

{
  "version": "0.1.1",
  "box_name": "A Fazenda - Ranking",
  "data": [
    {
      "__id": "f8c3500f39017602234a031caa64a8b4",
      "timestamp": 1408396531382,
      "name": "Rita Cadillac",
      "description": "Cracrete nº1",
      "picture": "http://im.r7.com/record/files/2C96/1617/3F6E/32AE/013F/72F9/AD72/3CE1/RitaCadillac1.jpg",
      "positive": 51638022,
      "negative": 18143089
    },
    {
      "__id": "7b1dd3f58be97715e9e06475bb58fce5",
      "timestamp": 1408396481826,
      "name": "Gominho",
      "description": "Fofoqueiro de Plantão",
      "picture": "http://im.r7.com/record/files/2C96/1618/3F6E/369D/013F/72F7/CF15/5F4B/Gominho1.jpg",
      "positive": "23249923",
      "negative": "39587707"
    },
    {
      "__id": "70580002438b08c63286d08b7c43fb4c",
      "timestamp": 1408396545027,
      "name": "Yudi Tamashiro",
      "description": "Apresentador e ídolo teen",
      "picture": "http://im.r7.com/record/files/2C96/1617/3F6E/32AE/013F/72FD/87BB/4436/Yudi1.jpg",
      "positive": 59089056,
      "negative": 14772265
    },
    {
      "__id": "3404c4a70e7704009cd1915a349189f4",
      "timestamp": 1408396555971,
      "name": "Andressa Urach",
      "description": "Personalidade da mídia",
      "picture": "http://im.r7.com/record/files/2C96/1618/3F6E/369D/013F/72EF/C598/41DC/Andressa1.jpg",
      "positive": null,
      "age": 32
    },
    {
      "__id": "c97686edbeb8df774a567e9884f4d490",
      "timestamp": 1408396562866,
      "name": "Bárbara Evans",
      "description": "Modelo e filha de Monique Evans",
      "picture": "http://im.r7.com/record/files/2C96/1618/3F6E/369D/013F/72F6/B48C/5BBD/B%C3%A1rbaraEvans1.jpg",
      "positive": 69274684,
      "negative": 9446548
    }
  ]
}

PS: I can not change any of these files.

    
asked by anonymous 21.06.2018 / 01:17

1 answer

0

You can use for...of in JSON for the array data and calculate the values (see explanations in the code) while adding two new entries in the objects: gostam and não gostam , each with their respective percentage in relation to the total votes (sum of positive and negative ).

I've created an illustrative table to show the results:

var json = {
  "version": "0.1.1",
  "box_name": "A Fazenda - Ranking",
  "data": [
    {
      "__id": "f8c3500f39017602234a031caa64a8b4",
      "timestamp": 1408396531382,
      "name": "Rita Cadillac",
      "description": "Cracrete nº1",
      "picture": "http://im.r7.com/record/files/2C96/1617/3F6E/32AE/013F/72F9/AD72/3CE1/RitaCadillac1.jpg",
      "positive": 51638022,
      "negative": 18143089
    },
    {
      "__id": "7b1dd3f58be97715e9e06475bb58fce5",
      "timestamp": 1408396481826,
      "name": "Gominho",
      "description": "Fofoqueiro de Plantão",
      "picture": "http://im.r7.com/record/files/2C96/1618/3F6E/369D/013F/72F7/CF15/5F4B/Gominho1.jpg",
      "positive": "23249923",
      "negative": "39587707"
    },
    {
      "__id": "70580002438b08c63286d08b7c43fb4c",
      "timestamp": 1408396545027,
      "name": "Yudi Tamashiro",
      "description": "Apresentador e ídolo teen",
      "picture": "http://im.r7.com/record/files/2C96/1617/3F6E/32AE/013F/72FD/87BB/4436/Yudi1.jpg",
      "positive": 59089056,
      "negative": 14772265
    },
    {
      "__id": "3404c4a70e7704009cd1915a349189f4",
      "timestamp": 1408396555971,
      "name": "Andressa Urach",
      "description": "Personalidade da mídia",
      "picture": "http://im.r7.com/record/files/2C96/1618/3F6E/369D/013F/72EF/C598/41DC/Andressa1.jpg",
      "positive": null,
      "age": 32
    },
    {
      "__id": "c97686edbeb8df774a567e9884f4d490",
      "timestamp": 1408396562866,
      "name": "Bárbara Evans",
      "description": "Modelo e filha de Monique Evans",
      "picture": "http://im.r7.com/record/files/2C96/1618/3F6E/369D/013F/72F6/B48C/5BBD/B%C3%A1rbaraEvans1.jpg",
      "positive": 69274684,
      "negative": 9446548
    }
  ]
}

for(var item of json.data){
   // pega os valores. Se for vazio ou null considera zero
   var posit = item.positive || 0;
   var negat = item.negative || 0;
   var total = parseInt(posit) + parseInt(negat); // soma os dois valores
   // pega a porcentagem e dispensa as decimais
   var gostam = ((posit*100) / total).toFixed(0);
   var naogostam = ((negat*100) / total).toFixed(0);
   
   // insere novas entradas no objeto
   item['gostam'] = isNaN(gostam) ? 0 : gostam;
   item['não gostam'] = isNaN(naogostam) ? 0 : naogostam;
}

// organiza o JSON com os que tem mais votos "gostei" em ordem decrescete
json.data.sort(function(a,b){
   return a.gostam < b.gostam;
});

// aqui irá inserir na tabela os resultados
var res = document.getElementById("resultado");
var html = '<caption>'+json.box_name+'</caption>'
    +'<tr><td>NOME</td><td>% GOSTAM</td><td>% NÃO GOSTAM</td></tr>';
for(var item of json.data){
   html += '<tr><td>'+item.name+'</td><td>'+item.gostam+'</td><td>'+item['não gostam']+'</td></tr>';
}

res.innerHTML = html;
td{
   text-align: center;
}
<table cellpadding="5" border="1" id="resultado"></table>
    
21.06.2018 / 02:24