2016-04-07 72 views
0

我想用lodash簡化這個函數,但是我不知道該怎麼做才能幫助我?用lodash簡化表達式

function _findMembers(usersAvailable,listOfUsers){ 
    console.log("listOfUsers") 
    console.log(listOfUsers) 
    var members = []; 
    for(var i=0; i<_.size(usersAvailable);i++){ 
     for(var j=0; j<_.size(listOfUsers); j++){ 
      if(usersAvailable[i].local.email == listOfUsers[j]){ 
       var user = usersAvailable[i]; 
       user.selected = true; 
       members.push(user); 
      } 
     } 
    } 
    console.log("members") 
    console.log(members) 
    return members; 
} 

回答

2

你並不需要使用lodash。只需使用標準的JavaScript過濾功能:

function _findMembers(usersAvailable, listOfUsers){  
    return usersAvailable.filter(function(userAvailable) { 
     return listOfUsers.indexOf(userAvailable.local.email) > -1; 
    }); 
}