gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:56:13 25 4
gpt4 key购买 nike

我在将以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com