<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script type="text/javascript">
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.toString = function(){
return "姓名:" + this.name + ",年龄:" + this.age + ",性别:" + this.age;
}
var one = new Person("青蛙",1,"男");
var two = new Person("牛蛙",2,"女");
var three = new Person("丑蛙",2,"女");
var four = new Person("美蛙",1,"女");
var five = new Person("香蛙",2,"女");
var arr = [one,two,three,four,five];
// 创建一个函数,可以将数组中满2岁的对象提取出来,然后封装到一个新的数组中返回
function getarr1(a){
// 步骤一:创建一个新的数组用来存放数据
var arr1 = [];
// 步骤二:遍历元素组所有元素,判断年龄是否大于等于二,是的话就向新数组添加这个元素
for(var i = 0;i < arr.length;i++){
if(arr[i].age >= 2){
arr1.push(arr[i]);
}
}
// 将新数组作为函数返回值返回
return arr1;
}
var result = getarr1(arr);
console.log(result);
</script>
</head>
<body>
</body>
</html>
运行结果: