简体   繁体   English

比较对象数组中的属性值

[英]comparing properties values inside an array of objects

so i'm doing some excercises as practice, since i'm a beginner, and i stumble into this problem that i can't solve, would anybody be kind to give me a hand? 因此,由于我是一名初学者,所以我正在练习一些练习,而我偶然遇到了这个我无法解决的问题,有人愿意帮我吗?

i would like to make a function that returns how many people is 18 or older, this is what i've been trying but i'm a bit confused.. 我想做一个返回18岁以上的人的函数,这是我一直在尝试的方法,但我有点困惑。

const examplePeople = [
  { name: 'John', age: 15 },
  { name: 'Jane', age: 16 },
  { name: 'Jack', age: 25 },
  { name: 'Ana', age: 18 },
  { name: 'Raul', age: 23 },
  { name: 'Pedro', age: 17 }
];

function countLegalPeople(people) {
  for (i= 0; i >= people["age"] ; i++){
    if (people[i]["age"] >= 18) {
      return people[i];
    }
  }
}

console.log(countLegalPeople(examplePeople));

I'd use reduce , where the accumulator is the number of objects found so far that pass the test: 我将使用reduce ,其中累加器是到目前为止找到的通过测试的对象数:

 const examplePeople = [ { name: 'John', age: 15 }, { name: 'Jane', age: 16 }, { name: 'Jack', age: 25 }, { name: 'Ana', age: 18 }, { name: 'Raul', age: 23 }, { name: 'Pedro', age: 17 } ]; const result = examplePeople.reduce((a, { age }) => a + (age >= 18), 0); console.log(result); 

With a for loop, you'd have to increment a more persistent variable, eg 使用for循环,您必须增加一个更持久的变量,例如

 const examplePeople = [ { name: 'John', age: 15 }, { name: 'Jane', age: 16 }, { name: 'Jack', age: 25 }, { name: 'Ana', age: 18 }, { name: 'Raul', age: 23 }, { name: 'Pedro', age: 17 } ]; let result = 0; for (let i = 0; i < examplePeople.length; i++) { if (examplePeople[i].age >= 18) { result++; } } console.log(result); 

But array methods are generally more terse and elegant IMO. 但是数组方法通常更简洁明了。

Why don't try Array.prototype.filter() 为什么不尝试Array.prototype.filter()

 const examplePeople = [ { name: 'John', age: 15 }, { name: 'Jane', age: 16 }, { name: 'Jack', age: 25 }, { name: 'Ana', age: 18 }, { name: 'Raul', age: 23 }, { name: 'Pedro', age: 17 } ]; function countLegalPeople(people) { return people.filter(p => p.age >= 18).length; } console.log(countLegalPeople(examplePeople)); 

The issue with your approach is that you are returning within your for loop. 方法的问题在于,您将在for循环中返回。 Whenever you return, your function will stop running (it essentially jumps out of your function), so, whenever you run into a person over the age of 18 you stop your loop and thus it cannot count any more people. 每当您返回时,您的函数都会停止运行(它实际上会跳出您的函数),因此,每当遇到18岁以上的人时,您都会停止循环,因此无法再计算更多的人了。

Instead, you can create a variable and set it to zero. 相反,您可以创建一个变量并将其设置为零。 This will count how many people of the age of 18 you have seen. 这将计算您见过多少18岁的人。 To do this, you will need to add one to this variable each time you see a person of an age of 18 or higher. 为此,每次看到18岁或以上的人时,您都需要在此变量中添加一个。 Then, once your for loop is complete, you can return this number. 然后,一旦您的for循环完成,您就可以返回该数字。

See example below: 请参见下面的示例:

 const examplePeople = [ { name: 'John', age: 15 }, { name: 'Jane', age: 16 }, { name: 'Jack', age: 25 }, { name: 'Ana', age: 18 }, { name: 'Raul', age: 23 }, { name: 'Pedro', age: 17 } ]; function countLegalPeople(people) { let counter = 0; // number of people >= 18; for (let i= 0; i < people.length; i++){ // loop through the array of people (using people.length) if (people[i]["age"] >= 18) { counter++; // age 1 to counter (same as counter = counter + 1) } } return counter; // return the amount of people >= 18 } console.log(countLegalPeople(examplePeople)); 

Your question was slightly confusing and could be taken two different ways, so if you are looking to return the total instances of people that are 18 and older in a new array, you would do something like this: 您的问题有点令人困惑,可以采用两种不同的方式,因此,如果您要返回一个新数组中18岁及以上的人员总数,则可以执行以下操作:

const examplePeople = [
  { name: 'John', age: 15 },
  { name: 'Jane', age: 16 },
  { name: 'Jack', age: 25 },
  { name: 'Ana', age: 18 },
  { name: 'Raul', age: 23 },
  { name: 'Pedro', age: 17 }
];

let adults = [];
for (let i = 0; i < examplePeople.length; i++) {
  if (examplePeople[i].age >= 18) {
    adults.push(examplePeople[i]);
  }
}
console.log(adults);

you can also get the length with a simple: 您还可以通过以下简单方法获得长度:

console.log(adults.length);

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

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