代码改变世界

利用循环遍历的方式判断某个对象是否属于这个数组

2016-03-30 22:29  流浪的诗人  阅读(1562)  评论(0编辑  收藏  举报

<!doctype html>
<html >
<head>
<title>利用循环遍历的方式判断某个对象是否属于这个数组</title>
<meta charset="UTF-8">
</head>
<body>
<!--
利用循环遍历的方式判断某个对象是否属于这个数组;
不属于这个数组时,把这个对象填充到数组里面去;
-->
</body>
</html>
<script type="text/javascript">
window.onload = function(){
window.inArray = new inArray();//对象初始化
var personArr = inArray.takeStudent();
console.log("personArr:"+personArr);
}
//构造函数,定义变量
function inArray(){
this.personArr = [];
this.studentArr = [2,12,34,56,10,25,100];
}
//原型链json对象,里面封装方法
inArray.prototype = {
getPersonSum:function(value){
for(var i = 0;i< this.personArr.length;i++){
if(this.personArr[i] == value){
return true;
}
}
return false;
},
takeStudent:function(){
for(var i = 0;i< this.studentArr.length;i++){
if(!inArray.getPersonSum(this.studentArr[i])){
inArray.personArr.push( this.studentArr[i]);
}
}
return inArray.personArr ;
}
}

</script>