<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//构建一个函数
function Person(name,age){
this.name=name;
this.age=age;
}
var p1=new Person("tom",9);
var p2=new Person("saly",14);
var p3=new Person("candy",10);
var p4=new Person("cherry",12);
//将上面的对象放入一个数组中
var arrP=[p1,p2,p3,p4];
//将数组中满十岁的元素提取出来
function tenPer(arr){
var newArr=[];
for(var i=0;i<=arr.length;i++){
if(arr[i].age>=10){
// console.log(arr.name);
newArr.push(arr[i])
}
}
return newArr;
}
console.log(tenPer(arrP))
</script>
</body>
</html>
报错原因是遍历的数组长度大于数组本身的长度,把for循环里的等于号去掉即可