簡體   English   中英

Lodash / JS-將_.find語句轉換為_.forEach

[英]Lodash/JS - Convert _.find statement to _.forEach

我無法將下面的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)
                        })
                    }
                }
            };
        }
    };
}]);

});

您可以只使用filter

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));

您可以_(groupList).groupBy('id').get(id)

 var groupList = [ { id: 1, name: 'site' }, { id: 2, name: 'test' }, { id: 2, name: 'prod' }, { id: 3, name: 'dev' }, { id: 4, name: 'back' }, { id: 4, name: 'front' }, { id: 5, name: 'sprint' } ]; console.log(_(groupList).groupBy('id').get(2)); console.log(_(groupList).groupBy('id').get(3)); console.log(_(groupList).groupBy('id').get(4)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM