javascript - Lodash/JS - 将 _.find 语句转换为 _.forEach

标签 javascript lodash

我在将以下 Lodash 语句转换为可在我在工作中继承的应用程序中运行的内容时遇到问题,并且正在尝试修复其中的错误。目前,我们的系统上只有一个设备在返回时遇到问题两个设备具有相同的名称,以下代码似乎是罪魁祸首,因为它只会返回数组中的第一个“true”值:

var group = _.find(groupList, {id: id});

如何成功地将其转换为迭代数组中所有对象然后返回这些对象的语句?我已尝试以下选项但无济于事:

 var group = _.filter(groupList, {id: id});

 var group = _.every(groupList, {id: id});

 var group = _.forEach(groupList, {id: id}) 
 return {id};

我知道我的语法中可能遗漏了一些东西。任何帮助将非常感激。运行Lodash v3.7.0

这是指令中的其余代码,以防有人感兴趣或看到我可能遗漏的其他内容:

define(['./../_module'], function (directives) {
'use strict';
directives.directive('dmGroupedList', ['$compile', '$state', 'APP_CONSTANTS', function ($compile, $state, APP_CONSTANTS) {
    return {
        restrict: 'A',
        scope: {
            items: '=',
            groupBy: '=',
            actions: '=',
            nonameGroupLabel: '='
        },
        templateUrl: function (elem, attrs) {
            return attrs.templateUrl || 'views/shared-templates/grouped-list.html';
        },
        link: function (scope, element, attrs) {
            scope.$watchGroup(['items', 'groupBy', 'nonameGroupLabel'], function () {
                scope.groupList = [];
                scope.groupedItems = {};

                var actions = scope.actions[scope.groupBy];

                _.forEach(scope.items, function (item) {
                    scope.handlers.getGroups(scope.groupList, item, scope.items, scope.groupBy, actions);
                });

                _.forEach(scope.groupList, function (group) {
                    var items = scope.groupedItems[group.id];
                    items = _.sortBy(items, function (item) {
                        return item.description;
                    });

                    scope.groupedItems[group.id] = items;
                });

                var groupsToSort = _.where(scope.groupList, {unassigned: false});
                var unassignedGroups = _.sortBy(_.where(scope.groupList, {unassigned: true}), 'name');
                scope.groupList = _.sortBy(groupsToSort, function (group) {
                    return group.name;
                });
                //adds unassigned groups to a new array via the javascript "push" method
                if (angular.isDefined(unassignedGroups)) {
                    for (var i = 0; i < unassignedGroups.length; i++) {
                        scope.groupList.push(unassignedGroups[i]);
                    }
                }
            });

            scope.handlers = {
                getGroups: function (groupList, item, items, groupBy, actions) {
                    var group = item[groupBy];

                    if (_.isEmpty(group)) {
                        scope.handlers.addGroupToList(groupList, APP_CONSTANTS.DEVICE_NONE_NAME_MAPPING.NONE, items, groupBy, item, actions, scope);
                    }
                    else {
                        if (angular.isArray(group) || angular.isObject(group)) {
                            _.forEach(group, function (groupName) {
                                if (groupName == APP_CONSTANTS.ZERO) {
                                    scope.handlers.addGroupToList(groupList, APP_CONSTANTS.DEVICE_NONE_NAME_MAPPING.NONE, items, groupBy, item, actions, scope);
                                    return;
                                }

                                scope.handlers.addGroupToList(groupList, groupName, items, groupBy, item, actions, scope);
                            })
                        } else {
                            scope.handlers.addGroupToList(groupList, group, items, groupBy, item, actions, scope);
                        }
                    }
                },
                addGroupToList: function (groupList, groupId, items, groupBy, item, handlers, scope) {
                    var id = _.camelCase(groupId);

                   var group = _.find(groupList, {id: id});
                    //var group = _.forEach(groupList, {id: id})
                    //return {id};

                    if (!group) {
                        var name = '';
                        var unassigned = false;
                        var link = null;

                        if (groupId == APP_CONSTANTS.DEVICE_NONE_NAME_MAPPING.NONE || groupId == APP_CONSTANTS.DEVICE_NONE_NAME_MAPPING.NONE_PARENT_ID) {
                            if (groupId == APP_CONSTANTS.DEVICE_NONE_NAME_MAPPING.NONE_PARENT_ID) {
                                name = APP_CONSTANTS.DEVICE_NONE_NAME_MAPPING.NONE;
                            } else {
                                name = APP_CONSTANTS.DEVICE_NONE_NAME_MAPPING.UNASSIGNED;
                            }

                            unassigned = true;
                        } else {
                            link = handlers.getGroupLink(groupId);
                            name = handlers.getGroupName(groupId, items);
                        }

                        group = {id: id, name: name, unassigned: unassigned, link: link};

                        groupList.push(group);
                    }

                    scope.groupedItems[group.id] = scope.groupedItems[group.id] || [];

                    if (angular.isDefined(handlers.processingGroup)) {
                        handlers.processingGroup(group, groupList, groupId, items, groupBy, item, handlers, scope);
                    } else {
                        scope.groupedItems[group.id].push({
                            description: handlers.getItemDescription(item),
                            link: handlers.getItemLink(item)
                        })
                    }
                }
            };
        }
    };
}]);

});

最佳答案

您可以只使用过滤器:

var group = groupList.filter((group) => group.id === id);

编辑:要仅返回元素,而不是数组,当只有一个匹配时,您可以执行以下操作:

var checkSingle = (groups) => groups.length === 1 ? groups[0] : groups;
var group = checkSingle(groupList.filter((group) => group.id === id));

关于javascript - Lodash/JS - 将 _.find 语句转换为 _.forEach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44935429/

相关文章:

javascript - 在 HTML 中创建压缩函数

javascript - 我的表单上的 React 生成按钮不断刷新页面

javascript - 如何使用node.js和firebase实现自动文本回复

javascript - iOS Safari/Chrome 中的 Cookie 持久性

javascript - 在嵌套数组中搜索并返回该数组中的值?

javascript - 将整数数组映射到对象数组

javascript - 当一个对象有额外的键时,如何使用 Lodash 比较 JSON 对象中的键和值?

javascript - 如何按对象的子对象对对象列表进行最佳分组?

backbone.js - 使用 Lodash 而不是 Underscore 和 Browserify 的主干

javascript - 在 alexa sdk v2 responsebuilder 中暂停