在基于值输出之前对JSON成员进行排序和for循环

时间:2019-07-01 05:56:29

标签: javascript arrays json sorting for-loop

我现在学习JavaScript已有1个月了,我正在创建一个用于学习目的的网页,但是我一直坚持根据较高的前10%和最低的10%来打印数据。 我已经在Repl.it上上传了所有内容,这是链接:https://repl.it/@YevgeniyMakkoye/Task2-RE

我正在谈论的页面在assets / js / getAttendance.js中

我正在使用的代码是:

let age = 20
print("I am \(age) years old")

但是在此代码下,您会发现我正在尝试的其他逻辑。 我该如何返回%Missed表?

这是fetchData.js,其中 var filteredMembers = []; actove = true; while (filteredMembers.length / jlen < 0.1 || !filteredMembers.length) { for(let member of members){ filteredMembers.push(member); } for (let i = 0; i < arrMemberVotes.length; i++) { if (arrMemberVotes[i] === arrMemberVotes) { arrMemberVotes.splice(i, 1); } } console.log(maxMinCalc(arrMissedVotes, false)); console.log(filteredMembers); } 是直接链接到JSON的JavaScript对象。

dataSenate/dataHouse

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。这是我用于根据特定值对对象进行排序的代码:

var filtredDataDesc = Object.values(json.members);
  filtredDataDesc.sort((a, b) => (b.votes_with_party_pct > a.votes_with_party_pct) ? 1 : -1);
  var filtredDataAsc = Object.values(json.members);
  filtredDataAsc.sort((a, b) => (a.votes_with_party_pct > b.votes_with_party_pct) ? 1 : -1);
  var desFullName = [];
  var ascFullName = [];


  for (var i = 0; i <= 10; i++) {

    fullNameDesc = filtredDataDesc[i].first_name + " " + filtredDataDesc[i].middle_name + " " + filtredDataDesc[i].last_name;

    if(fullNameDesc.includes("null") == true){
      fullNameDesc = filtredDataDesc[i].first_name + " " + filtredDataDesc[i].last_name;
    } else {
      fullNameDesc = filtredDataDesc[i].first_name + " " + filtredDataDesc[i].middle_name + " " + filtredDataDesc[i].last_name;
    }

    fullNameAsc = filtredDataAsc[i].first_name + " " + filtredDataAsc[i].middle_name + " " + filtredDataAsc[i].last_name;
    if(fullNameAsc.includes("null") == true){
      fullNameAsc = filtredDataAsc[i].first_name + " " + filtredDataAsc[i].last_name;
    } else {
      fullNameAsc = filtredDataAsc[i].first_name + " " + filtredDataAsc[i].middle_name + " " + filtredDataAsc[i].last_name;
    }
    globalVariables();

    topTarget.innerHTML += oTr + oTd + fullNameDesc + cTd + oTd + filtredDataDesc[i].missed_votes + cTd + oTd + filtredDataDesc[i].votes_with_party_pct + cTd + cTr;
    bottomTarget.innerHTML += oTr + oTd + fullNameAsc + cTd + oTd + filtredDataAsc[i].missed_votes + cTd + oTd + filtredDataAsc[i].votes_with_party_pct + cTd + cTr;