我做了一个程序,显示我的'组'阵列上有多少“男人”。到目前为止我得到了这个:
Person.prototype.myFunction= function(group){
if (this.gender = 'male') {
console.log(group);
}
else{
return Person;
}
};
var person1 = new Person('Alex', 'male', '25');
var person2 = new Person('Roberto', 'male', '70');
var person3 = new Person('Silverio', 'male', '45');
var person4 = new Person('Arturo', 'male', '24');
var person5 = new Person('Ramón', 'male', '50');
var person6 = new Person('Sol', 'female', '25');
var person7 = new Person('Sarai', 'female', '20');
var person8 = new Person('Alexa', 'female', '22');
var person9 = new Person('Betty', 'female', '24');
var person10 = new Person('Daniela', 'female', '25');
var group=[person1, person2, person3, person4, person5, person6, person7, person8, person9, person10];
我确信我很接近,但是如果它不能正常工作的话。
答案 0 :(得分:3)
使用for loop来迭代论坛,如果当前的人是男性,请在result
添加一个:
var group = [
new Person('Alex', 'male', '25'),
new Person('Roberto', 'male', '70'),
new Person('Silverio', 'male', '45'),
new Person('Arturo', 'male', '24'),
new Person('Ramón', 'male', '50'),
new Person('Sol', 'female', '25'),
new Person('Sarai', 'female', '20'),
new Person('Alexa', 'female', '22'),
new Person('Betty', 'female', '24'),
new Person('Daniela', 'female', '25')
];
function countMales(group){
var result = 0;
for(var i = 0; i < group.length; i++) {
if(group[i].gender === 'male') result += 1;
}
return result;
};
console.log(countMales(group));
function Person(name, gender, age) {
this.name = name;
this.gender = gender;
this.age = age;
}
&#13;
答案 1 :(得分:1)
使用reduce
功能,您可以使用gender === 'male'
ES6方法:
function Person(name, gender, age) { this.name = name; this.gender = gender; this.age = age;}var person1 = new Person('Alex', 'male', '25');var person2 = new Person('Roberto', 'male', '70');var person3 = new Person('Silverio', 'male', '45');var person4 = new Person('Arturo', 'male', '24');var person5 = new Person('Ramón', 'male', '50');var person6 = new Person('Sol', 'female', '25');var person7 = new Person('Sarai', 'female', '20');var person8 = new Person('Alexa', 'female', '22');var person9 = new Person('Betty', 'female', '24');var person10 = new Person('Daniela', 'female', '25');var group=[person1, person2, person3, person4, person5, person6, person7, person8, person9, person10];
var howMany = group.reduce((a, {gender}) => (a + (gender === 'male')), 0);
console.log(howMany);
&#13;
使用简单for-of-loop
function Person(name, gender, age) { this.name = name; this.gender = gender; this.age = age;}var person1 = new Person('Alex', 'male', '25');var person2 = new Person('Roberto', 'male', '70');var person3 = new Person('Silverio', 'male', '45');var person4 = new Person('Arturo', 'male', '24');var person5 = new Person('Ramón', 'male', '50');var person6 = new Person('Sol', 'female', '25');var person7 = new Person('Sarai', 'female', '20');var person8 = new Person('Alexa', 'female', '22');var person9 = new Person('Betty', 'female', '24');var person10 = new Person('Daniela', 'female', '25');var group=[person1, person2, person3, person4, person5, person6, person7, person8, person9, person10];
var howMany = 0;
for (let person of group) {
if (person.gender === 'male') howMany++;
}
console.log(howMany);
&#13;