function(people){
}
people([{name: 'John', age: 22},{name: 'paul', age: 23},{name: 'mathew', age: 24},])
如何返回由22岁以上的人组成的数组的长度,应该为2。
答案 0 :(得分:0)
您可以使用array reduce进行计数:
function people(array){
return array.reduce((total, current)=>{
if(current.age>22)
total = total +1;
return total;
},0);
}
console.log(people([{name: 'John', age: 22},{name: 'paul', age: 23},{name: 'mathew', age: 24},]));
答案 1 :(得分:0)
下面是针对您的问题的功能编程方法:
const persons = [
{name: 'John', age: 22},
{name: 'paul', age: 23},
{name: 'mathew', age: 24}
]
const over22 = persons.filter(person => person.age > 22)
console.log('there are', over22.length, 'people aged over 22')
希望这会有所帮助。 干杯,
答案 2 :(得分:0)
请尝试以下操作:
function find_length(checkField, valueField, myArray) {
for (var i = 0; i < myArray.length; i++) {
if (myArray[i][checkField] == valueField) {
return Object.keys(myArray[i]).length;
}
}
}
var myList = [{name: 'John', age: 22}, {name: 'paul', age: 23}, {name: 'mathew', age: 24},];
var ageObjLength = find_length('age', '22', myList );
alert("Length is " + ageObjLength);