简体   繁体   English

如何根据 JavaScript 中的给定条件过滤对象?

[英]How to filter objects based on given condition in JavaScript?

I'm new to this community and just started programming I couldn't find anything about this topic can anyone please solve this.我是这个社区的新手,刚开始编程我找不到关于这个主题的任何信息,任何人都可以解决这个问题。 I need to filter names who's each points in array above 75. i have tried this and unable iterate over an array.我需要过滤数组中每个点都超过 75 的名称。我已经尝试过这个并且无法迭代数组。

 const candidatesList = [{'name':'Blake Hodges', 'points':[76,98,88,84]},{'name':'James Anderson', 'points':[0,98,12,13]}] const arrayOfSelecetedCandidates=[] for(let object of candidatesList){ if (candidatesList.every(candidatesList.points>75)){ arrayOfSelecetedCandidates.push(candidatesList.name) } } console.log(arrayOfSelecetedCandidates);

Maybe you want this?也许你想要这个?

 const candidatesList = [{'name':'Blake Hodges', 'points':[76,98,88,84]},{'name':'James Anderson', 'points':[0,98,12,13]}] const arrayOfSelecetedCandidates=[] for(let object of candidatesList){ if (object.points.every(i=>i>75)) arrayOfSelecetedCandidates.push(object.name) } console.log(arrayOfSelecetedCandidates);

But as @Dai pointed out filter is always better if you want to test an array and return item that pass the test:但正如@Dai 指出的那样,如果你想测试一个数组并返回通过测试的项目,过滤器总是更好:

 const candidatesList = [{'name':'Blake Hodges', 'points':[76,98,88,84]},{'name':'James Anderson', 'points':[0,98,12,13]}] const arrayOfSelecetedCandidates=candidatesList.filter(i=>i.points.every(y=>y>75)) console.log(arrayOfSelecetedCandidates)

You can achieve this by using Array.filter() method along with Array.every() to test whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.您可以通过使用Array.filter()方法和Array.every()来测试数组中的所有元素是否通过提供的 function 实现的测试来实现这一点。它返回一个 Boolean 值。

Working Demo:工作演示:

 const candidatesList = [{ 'name': 'Blake Hodges', 'points': [76, 98, 88, 84] }, { 'name': 'James Anderson', 'points': [0, 98, 12, 13] }]; let res = candidatesList.filter((obj) => { return obj.points.every((item) => item > 75) }); console.log(res);

"I need to filter names who's each points in array above 75" “我需要过滤数组中每个点都在 75 以上的名字”

Looking at the content of OP, I believe that's incorrect.查看OP的内容,我认为这是不正确的。 It would make more sense if the desired output would be only the objects (aka candidates) that have an average of points at or above 75 not a total of 75 (although not explained very well).如果所需的 output 只是平均点数等于或高于 75 的对象(又名候选对象)而不是总共 75 点(尽管解释得不是很好),那将更有意义。 So the plan of attack would be:所以攻击计划是:

  • Get each of the candidates with a for...of loop:使用for...of循环获取每个候选人:

     for (const candidate of candidates) {...
  • Get each of the candidate's points ( candidate.points ) and get their sums using .reduce() (note: the return goes to a new property called .total ).获取每个候选点 ( candidate.points ) 并使用.reduce()获取它们的总和(注意:返回转到一个名为.total的新属性)。 Then take that total and divide by the number of grades found in points array (it's candidate.points.length = 4):然后取该总数并除以 points 数组中的成绩数(它是candidate.points.length = 4):

     candidate.total = candidate.points.reduce((sum, cur) => sum + cur, 0); candidate.gpa = Math.floor(candidate.total / candidate.points.length);
  • Once candidate.gpa is established, .filter() will determine if it is equal to or greater than 75.一旦candidate.gpa建立, .filter()将确定它是否等于或大于 75。

     return candidates.filter(candidate => candidate.gpa >= 75);

 const candidates = [{ 'name': 'Blake Hodges', 'points': [76, 98, 88, 84] }, { 'name': 'James Anderson', 'points': [0, 98, 12, 13] }, { 'name': 'Zer0 0ne', 'points': [100, 88, 91, 84] }, { 'name': 'Ronald McDonald', 'points': [72, 51, 8, 89] }]; const selected = candidates => { for (const candidate of candidates) { candidate.total = candidate.points.reduce((sum, cur) => sum + cur, 0); candidate.gpa = Math.floor(candidate.total / candidate.points.length); } return candidates.filter(candidate => candidate.gpa >= 75); }; console.log(selected(candidates));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM