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

Asked

Viewed 174 times

1

Save, I have a JSON file with variables "positives" and "negatives", I would like to transform these values in percentage, "positives" being "like" and "negatives" being "don’t like". After that as I could organize the profiles based on these 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’t change any of these files.

  • "Organize" in what sense?

  • Give Sort in the items, depending on the most voted positive put it first in the list and the least voted last positive in the list

  • 100% would be the sum of the positive and negative?

  • But what are these values in positive and negative ? They certainly don’t look like counts.

  • Do you already have the list? It’s an HTML already ready or you want to create one after the results?

  • @dvd I have a list yes, but with other values

  • With the solution I put in response you can organize. In addition to entering the percentages into the objects, I reorganized the array in order of votes. With this you can assemble the list as you wish.

Show 2 more comments

1 answer

0


Can use a for...of in JSON by array data and go calculating the values (see explanations in the code) at the same time adding two new entries in the objects: gostam and não gostam, each with its respective percentage in relation to the total votes (sum of positive and negative).

I 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>

  • Cara loved everything, but there would be no way to make a solution that did not edit the json and do with Retrieve in html?

  • You can make a copy of json: var copia = JSON.parse(JSON.stringify(json));

  • Making a copy, you change only the copy and leave the original intact. Does it serve like this? I update the response.

  • It does, thank you

  • Ready. Updated response! Abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.