获取具有最高属性值的对象的最佳方法 [英] Best Way to Get Objects with Highest Property Value

查看:69
本文介绍了获取具有最高属性值的对象的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下多维的学生对象数组:

I have the following multidimensional array of student objects:

var students = [
{name: "Jack", age: "NYN", attempts: 3, wrong: 2},
{name: "Phil", age: "NNNY", attempts: 4, wrong: 3},
{name: "Tom", age: "", attempts: 0, wrong: 0},
{name: "Lucy", age: "YYNY", attempts: 4, wrong: 1},
{name: "Ben", age: "NYNN", attempts: 4, wrong: 3},
{name: "Hardest", age: "NNN", attempts: 3, wrong: 3}
]

我正在尝试创建一个数组"peopleMostNeedingHelp",该数组仅包含具有最高错误"属性值的学生.因此,"peopleMostNeedingHelp"应仅包含对象Phil,Ben和Hardest.问题是,我做的方式还包括不需要的杰克",因为他是第一个比较对象.

I'm trying to create an array 'peopleMostNeedingHelp' which only comprises of the students with the highest 'wrong' property values. So 'peopleMostNeedingHelp' should only contain the objects Phil, Ben and Hardest. The problem is, the way I've done it also includes the unwanted 'Jack' as he is the first comparison.

我如何创建一个仅返回答案最错误的人的函数?

How can I create a function that only returns the people with the most wrong answers?

var s2 = "Jack:NYN,Phil:NNNY,Tom:,Lucy:YYNY,Ben:NYNN,Hardest:NNN";
var s2Arr = s2.split(','); // convert string to an array
var s2MdArr = s2Arr.map(function(e) {return e.split(':'); }); // convert to MD array
var totalWrongAnswers = 0;

for(i=0; i < s2Arr.length; i++) {
    var attempts = s2MdArr[i][1].length;
	var noWrong = (s2MdArr[i][1].match(/N/g) || []).length;
	s2MdArr[i].push(attempts); // add to array[i][2]
	s2MdArr[i].push(noWrong); // add to array[i][3]
	totalWrongAnswers += noWrong; // update total wrong
}

var s2ArrObj = s2MdArr.map(function(e) { return {name: e[0], age: e[1], attempts: e[2], wrong: e[3]} }); // create objects in MD Array

    var firstPerson = s2ArrObj[0]; // initialise so can make a comparison
    var person = firstPerson;
    var peopleMostNeedingHelp = [];
// update person to the person with the highest no. of wrong answers
function something() {
for (i = 0; i < s2ArrObj.length; i++) { // for each person
    if (s2ArrObj[i].wrong >= person.wrong) { // problem = first instance always true
        person = s2ArrObj[i]; // update person variable so can compare next person
        peopleMostNeedingHelp.push(person);
    }
}
}

something();
console.log(peopleMostNeedingHelp);

推荐答案

实现目标最简单的算法是:

The most straight-forward algorithm to achieve your goal would be:

  1. students数组中找到wrong的最大值.
  2. 使用filter仅离开相关的学生(属性wrong等于最大值).
  1. Find the maximum value for wrong within students array.
  2. Use filter to leave only the relevant students (with the property wrong equals the maximum).

var students = [{name: "Jack", age: "NYN", attempts: 3, wrong: 2},{name: "Phil", age: "NNNY", attempts: 4, wrong: 3},{name: "Tom", age: "", attempts: 0, wrong: 0},{name: "Lucy", age: "YYNY", attempts: 4, wrong: 1},{name: "Ben", age: "NYNN", attempts: 4, wrong: 3},{name: "Hardest", age: "NNN", attempts: 3, wrong: 3}];

// Find the maximum 'wrong' value
let maxValue = 0;
for(let student of students) {
  if(student.wrong > maxValue) {
    maxValue = student.wrong;
  }
}

// filter out the students with 'wrong' value different than the maximum
let onlyMax = students.filter(item => item.wrong == maxValue);
console.log(onlyMax);

请注意,所有算法所做的操作都是将数组迭代两次,从而导致运行时间为 O(2n)= O(n).

Note all the algorithm does is iterating the array twice, resulting in run-time of O(2n) = O(n).

更通用的解决方案允许在对象数组中找到最大值为property的项目:

The more general solution allows one to find the items with maximum value of property in an objects array:

var students = [{name: "Jack", age: "NYN", attempts: 3, wrong: 2},{name: "Phil", age: "NNNY", attempts: 4, wrong: 3},{name: "Tom", age: "", attempts: 0, wrong: 0},{name: "Lucy", age: "YYNY", attempts: 4, wrong: 1},{name: "Ben", age: "NYNN", attempts: 4, wrong: 3},{name: "Hardest", age: "NNN", attempts: 3, wrong: 3}];

function filterByMax(arr, property) {
  // Find the maximum 'wrong' value
  let maxValue = 0;
  for(let item of arr) {
    if(item[property] > maxValue) {
      maxValue = item[property];
    }
  }
  
  // filter out the students with 'wrong' value different than the maximum
  return arr.filter(item => item[property] == maxValue);
}

console.log(filterByMax(students, 'wrong'));

这篇关于获取具有最高属性值的对象的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆