用lodash简化表达

时间:2016-04-07 09:24:47

标签: javascript algorithm 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 个答案:

答案 0 :(得分:2)

您不需要使用lodash。只需使用标准的javascript过滤功能:

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

答案 1 :(得分:1)

使用loash的lodash过滤功能

并包含javascript函数

您可以尝试这样的事情:

var members = usersAvailable.filter(function (currentUser) {
        if(listOfUsers.includes(currentUser.local.email)){
            currentUser.selected=true;
            return true;
        }else{
            return false
        }
        return listOfUsers.includes(currentUser.local.email);
    });