使用map / filter / reduce根据百分比对用户进行排名

时间:2015-09-08 14:37:52

标签: javascript

我希望根据用户的百分比显示用户的所有详细信息,并为其分配排名。在这里,我得到了所有的细节。如何对用户进行排序和打印,如。

Tom has scored marks in Science - 80 Maths - 89 English - 91 and secured 86.67 % and his rank is 1.  

同样,学生中的每个用户都反对。

var students = [{
  name: "John",
  age: 20,
  id: 1,
  marks: {
    science: 80,
    maths: 90,
    english: 67
  }
}, {
  name: "Jack",
  age: 22,
  id: 3,
  marks: {
    science: 56,
    maths: 91,
    english: 81
  }
}, {
  name: "Robert",
  age: 23,
  id: 2,
  marks: {
    science: 75,
    maths: 79,
    english: 87
  }
}, {
  name: "Tom",
  age: 20,
  id: 4,
  marks: {
    science: 80,
    maths: 89,
    english: 91
  }
}];


var inDetail = getPercentageAndRank(students);

function getPercentageAndRank(students) {

  var details = students.map(function(getMarks) {
    console.log(((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3));
    return {
      name: getMarks.name,
      science: getMarks.marks.science,
      maths: getMarks.marks.maths,
      english: getMarks.marks.english,
      percentage: ((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3)
    };
  })

  return details.map(function(gg) {
    return [
      gg.name,
      "has scored marks in Science -",
      gg.science,
      "Maths -",
      gg.maths,
      "English -",
      gg.english,
      "and secured", (gg.percentage).toFixed(2),
      "%."
    ].join(' ');

  });
}

输出:

["John has scored marks in Science - 80 Maths - 90 English - 67 and secured 79.00 %.",
    "Jack has scored marks in Science - 56 Maths - 91 English - 81 and secured 76.00 %.",
    "Robert has scored marks in Science - 75 Maths - 79 English - 87 and secured 80.33 %.",
    "Tom has scored marks in Science - 80 Maths - 89 English - 91 and secured 86.67 %."]

这里我得到的输出不是按照上面说的。我怎么能得到如上所述..

1 个答案:

答案 0 :(得分:2)

您需要添加:

.sort(function(a, b) {
    return b.percentage - a.percentage;
  });

检查此解决方案。

var students = [{
  name: "John",
  age: 20,
  id: 1,
  marks: {
    science: 80,
    maths: 90,
    english: 67
  }
}, {
  name: "Jack",
  age: 22,
  id: 3,
  marks: {
    science: 56,
    maths: 91,
    english: 81
  }
}, {
  name: "Robert",
  age: 23,
  id: 2,
  marks: {
    science: 75,
    maths: 79,
    english: 87
  }
}, {
  name: "Tom",
  age: 20,
  id: 4,
  marks: {
    science: 80,
    maths: 89,
    english: 91
  }
}];


var inDetail = getPercentageAndRank(students);

function getPercentageAndRank(students) {

  var details = students.map(function(getMarks) {
    // console.log(((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3));
    return {
      name: getMarks.name,
      science: getMarks.marks.science,
      maths: getMarks.marks.maths,
      english: getMarks.marks.english,
      percentage: ((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3),
      rank: 0
    };
  }).sort(function(a, b) {
    return b.percentage - a.percentage;
  });

  for (var i = 0; i < details.length; i++) {
    // Updating the rank in order.
    details[i].rank = i + 1;
  }

  return details.map(function(gg) {
    return [
      gg.name,
      "has scored marks in Science -",
      gg.science,
      "Maths -",
      gg.maths,
      "English -",
      gg.english,
      "and secured", (gg.percentage).toFixed(2),
      "%", "and his rank is", gg.rank + "."
    ].join(' ');

  }).reduce(function(previous, current) {
    return previous + '\n' + current;
  }); // String result...
}
console.log(inDetail);

在这个问题中,你不需要过滤功能。