如果profile.team_member_designations是一个数组,则下面的代码运行。我想添加一个条件来过滤或检查profile_member_designations是否为数组,因为如果它是字符串,则我不希望代码运行。本质上,就在map函数之前,我想过滤掉数组以仅通过代码运行。我该怎么做呢?

JS

   var getTeamDesignations = function(profile) {
        //Designations for a single team member
        var teamDesignations = [];
        if (profile.team_members) {
            teamDesignations = _.chain(profile.team_members)
                .map(_.property('team_member_designations'))
                .flatten()
                .uniq()
                .value();
        }
        return teamDesignations;
    };

最佳答案

如果我正确理解了您的问题,那么您想要这样的事情。

var getTeamDesignations = function(profile) {
    //Designations for a single team member
    var teamDesignations = [];
    if (profile.team_members) {
        teamDesignations = _.chain(profile.team_members)
            .filter(_.compose(_.isArray, _.property('team_member_designations')))
            .map(_.property('team_member_designations'))
            .flatten()
            .uniq()
            .value();
    }
    return teamDesignations;
};

10-07 19:56