返回数组的最高值,Javascript函数的结果

时间:2018-04-04 23:32:05

标签: javascript sorting

我正在制作一个程序,可以从我的平均值中返回最高的5个值。功能

这是我的代码:

Person.prototype.average = function(){

    var plus=0;
    for (var i = 0; i < this.kilometers.length; i++) {
        plus += this.kilometers[i];
    }
    var total= plus/this.kilometers.length;
    return total;
};

function myFunction(runners) {
  var list = [];
  var plus = 0;
  for (var i = 0; i < runners.length; i++) {
    console.log(runners[i].average());
  }
}

var runners = [new Person('Alex', 'male', '25', [4, 3, 3, 3, 5, 2.9, 4.2]),
  new Person('Roberto', 'male', '70', [5, 4, 5, 3.2, 4.3, 5, 3]),
  new Person('Silverio', 'male', '45', [3, 3, 3, 3, 3, 3, 4]),
  new Person('Ramon', 'male', '50', [5, 5, 5, 5, 4, 3, 3]),
  new Person('Arturo', 'male', '24', [3, 3.4, 4, 5, 4.9, 2.9, 4, 3]),
  new Person('Sol', 'female', '25', [4, 3, 4, 3, 4, 3, 4]),
  new Person('Sarai', 'female', '20', [5, 5, 5, 5, 5, 5, 3]),
  new Person('Alexa', 'female', '22', [4, 3, 2, 4, 4, 5, 3.8]),
  new Person('Betty', 'female', '24', [3.1, 3.2, 2.9, 2, 3.3, 3.4, 2.8]),
  new Person('Daniela', 'female', '25', [5, 5, 4, 3, 4, 2.8, 5])
];

我知道我必须先使用排序方法,但我不知道该怎么做。

3 个答案:

答案 0 :(得分:0)

Person.prototype.average = function() {
    var plus = 0;
    for(var i=0;i<this.kilometers.length;i++){
        plus += this.kilometers[i];
    }
    var total = plus/this.kilometers.length;
    return this.kilometers.slice().sort().slice(-5); // you have to slice first if you don't want to modify original array
}

以下是使用方法的文档:

答案 1 :(得分:0)

在比较两个值时,您可以将函数传递给调用array.sort()函数的average()函数。例如:

let averages = runners.sort((a,b) => a.average() - b.average())

现在averages将有一个按平均值排序的Person列表。然后,您可以slice列出前五名。

averages.slice(0,5)

这是一个更完整的例子:

&#13;
&#13;
function Person(name, gender, age, scores){
    this.name = name;
    this.gender = gender;
    this.age  = age;
    this.scores = scores;
}
Person.prototype.average = function(){
    // shorter average function
    return this.scores.reduce((a,c) => a+c, 0)/this.scores.length
}

var runners = [new Person('Alex', 'male', '25', [4, 3, 3, 3, 5, 2.9, 4.2]),
  new Person('Roberto', 'male', '70', [5, 4, 5, 3.2, 4.3, 5, 3]),
  new Person('Silverio', 'male', '45', [3, 3, 3, 3, 3, 3, 4]),
  new Person('Ramon', 'male', '50', [5, 5, 5, 5, 4, 3, 3]),
  new Person('Arturo', 'male', '24', [3, 3.4, 4, 5, 4.9, 2.9, 4, 3]),
  new Person('Sol', 'female', '25', [4, 3, 4, 3, 4, 3, 4]),
  new Person('Sarai', 'female', '20', [5, 5, 5, 5, 5, 5, 3]),
  new Person('Alexa', 'female', '22', [4, 3, 2, 4, 4, 5, 3.8]),
  new Person('Betty', 'female', '24', [3.1, 3.2, 2.9, 2, 3.3, 3.4, 2.8]),
  new Person('Daniela', 'female', '25', [5, 5, 4, 3, 4, 2.8, 5])
];

let averages = runners.sort((a,b) => b.average() - a.average())

let topFive = averages.slice(0,5)
console.log(topFive.map(n => `name: ${n.name}: Average: ${n.average()}`))
&#13;
&#13;
&#13;

答案 2 :(得分:0)

function myFunction(a, b) {
    return a.average() > b.average() ? -1 : 1;
}

runners.sort(myFunction).splice(0, 5)

// for example
function Person(name) {
	this.name = name;
}

Person.prototype.average = function(){
    var plus=0;
    for (var i = 0; i < this.kilometers.length; i++) {
        plus += this.kilometers[i];
    }
    var total= plus/this.kilometers.length;
    return total;
};

var runners = [new Person('Alex', 'male', '25', [4, 3, 3, 3, 5, 2.9, 4.2]),
  new Person('Roberto', 'male', '70', [5, 4, 5, 3.2, 4.3, 5, 3]),
  new Person('Silverio', 'male', '45', [3, 3, 3, 3, 3, 3, 4]),
  new Person('Ramon', 'male', '50', [5, 5, 5, 5, 4, 3, 3]),
  new Person('Arturo', 'male', '24', [3, 3.4, 4, 5, 4.9, 2.9, 4, 3]),
  new Person('Sol', 'female', '25', [4, 3, 4, 3, 4, 3, 4]),
  new Person('Sarai', 'female', '20', [5, 5, 5, 5, 5, 5, 3]),
  new Person('Alexa', 'female', '22', [4, 3, 2, 4, 4, 5, 3.8]),
  new Person('Betty', 'female', '24', [3.1, 3.2, 2.9, 2, 3.3, 3.4, 2.8]),
  new Person('Daniela', 'female', '25', [5, 5, 4, 3, 4, 2.8, 5])
];

function myFunction(a, b) {
    return a.average > b.average ? -1 : 1;
}

console.log(runners.sort(myFunction).splice(0, 5));