For循环的演变

For循环,For In,For each,$Angular.forEach,$.grep

1. For循环

for(var i=0;i<10;i++){
console.log(i);
}

2.For in

var list=[{name:'test1'},{name:'test2'}];
for(var m in list){
console.log(m)}

3.For each

var database = {users: ["CSSer", "John", "David"],    
sendEmail: function (user) {
if (this.isValidUser(user)) {       
 console.log("test");    }},   
 isValidUser: function (user) {       
 console.log("test1");      
 return true;    }};// 向每个用户发送邮件 database.users.forEach(database.sendEmail,database);

4.Angular.forEach ,Angular $.grep

$scope.userList=[{name:'t1',age:10},{name:'t1',age:10},{name:'t1',age:10}];
$scope.oldUserList=[{name:'t1',age:10},{name:'t1',age:10},{name:'t1',age:10}];
angular.forEach($scope.userList,function(user){ //user是前面的迭代对象
var tempIndex;
$.grep($scope.oldUserList,function(oldUser,index){
if(oldUser.id==user.id){
tempIndex=index;
$scope.oldUserList.splice(tempindex,1);
}
});

});

你可能感兴趣的:(For循环的演变)