2017-07-05 65 views
0

我無法將以下Lodash語句轉換爲在我繼承的應用程序中工作的某些東西,並且正在嘗試修復錯誤。此刻,我們有隻用一個我們的系統,當兩個設備具有相同的名稱返回的設備問題和下面的代碼似乎是因爲它只會在數組中返回的第一個「真」值的罪魁禍首:Lodash/JS - 將_.find語句轉換爲_.forEach

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

});

+0

如果'var group = _.find(groupList,{id:id});''然後'var group = _.filter(groupList,{id:id});'應該有效。 –

+0

我認爲你應該添加你正在使用的lodash版本,v2,v3和v4之間有很大的區別 – zooblin

+0

v3.7.0是版本 – SDH

回答

1

你可以用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)); 
+0

我試過這個確切的代碼,但它打破了應用程序。你知道{id:id}在上面的代碼中代表什麼嗎?在AngularJS中運行的應用程序也是有幫助的。同樣,我沒有創建原始代碼 – SDH

+0

@SDH也認爲最初的'.find()'返回一個_single_值,並返回一個_array_。這個建議本身可能不是破壞應用程序的東西,但假設'group'的代碼的其他部分是帶有'id'的單個組。 –

+0

@SDH這可能是Patrick說的!檢查我編輯的答案。 –

0

您可以_(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>