1

我在这里遇到问题,需要一些指导。我选择了我的选择,并从两个不同的Angular数据中提取匹配结果。例如,我有两个角度范围,一个叫做$scope.mSessions,另一个是$scope.cSessions。每个阵列都有不同的键,除了它们共享相同的类别,并根据我的选择使用我的<select>标记拉取常用数据。所以我<select>选项将有类别,如RED FRUITYELLOW FRUITORANGE FRUIT,如果我选择RED FRUIT,它会经过我的阵列中mSessionscSessions然后拉起"m_category": ["Apple", "Strawberry", "Pineapple"]"category": [{"RED":["YES"]}]。我想我应该创建一个新的数组,将两个数据合并为一个,然后进行字符串比较,或者通过选择其中一个下拉列表来访问两个不同的数据。我无法弄清楚做这件事的最好方法是什么。请帮忙..!从下拉菜单中检索组合结果,但是从两个不同的角阵列中获取组合结果

这里是我的代码的jsfiddle第一http://jsfiddle.net/missggnyc/ujj18nhv/29/

HTML

<div ng-app="myFruit"> 
    <div ng-controller="fruitController"> 
    <select ng-model="selectedAnswer" ng-options="c.cat for c in mySelection"></select> {{selectedAnswer}} 
    <table> 
     <tr> 
     <td>Session Name</td> 
     <td>M Category</td>  
     </tr> 
     <tr ng-repeat="m in mSessions">   
     <td>{{m.name}}</td> 
     <td>{{m.m_category}}</td>   
     </tr> 
    </table> 
    <table> 
     <tr> 
     <td>C Category</td> 
     </tr> 
     <tr ng-repeat="c in cSessions ">   
     <td>{{c.category}}</td>   
     </tr> 
    </table> 
    </div> 
</div> 

JS

var app = angular.module("myFruit", []); 
    app.controller("fruitController", function($scope) { 
      $scope.mySelection = [ 
    {"cat": "RED FRUIT", "m_category": ["Apple", "Strawberry", "Pineapple"], "category": [{"RED":["YES"]}] }, 
    {"cat": "YELLOW FRUIT", "m_category": ["Banana", "Pineapple"], "category": [{"YELLOW": ["YES"]}] }, 
    {"cat": "ORANGE FRUIT", "m_category": ["Peach", "Nectarine"], "category": [{"ORANGE": ["YES"]}]} 
    ]; 
    $scope.mSessions = [{ 
    "id": 2, 
    "name": "BD20", 
    "m_category": ["Apple", "Strawberry", "Pineapple"] 
    }, { 
    "id": 3, 
    "name": "CS03", 
    "m_category": ["Banana", "Pineapple"] 
    }, { 
    "id": 4, 
    "name": "MIS99", 
    "m_category": ["Peach", "Nectarine"] 
    }]; 

    $scope.cSessions = [{ 
    "number": 439, 
    "name": "FDFG", 
    "category": [{"RED":["YES"]}] 
    }, { 
    "number": 34, 
    "name": "CN", 
    "category": [{"YELLOW": ["YES"]}] 
    }, { 
    "number": 44, 
    "name": "PPP", 
    "category": [{"ORANGE": ["YES"]}] 
    }]; 
}); 
+0

你可以'flatMap'所有类别组合在一起来填充单阵列与人l密钥的排列,但这对你可能做的事情似乎并不有用。请参阅https://jsfiddle.net/fo0aoc9f/以获取如何执行此操作的示例(尽管您可能希望使用来自生产中的Lodash等高性能库的flatMap和uniq实现)。 – miqid

+0

@miqid,感谢您的输入。但是我不熟悉'flatMap'和'uniq'方法,因为它们是更多的Java方法。我还更新了我的'$ scope.mySelection'数组,因为我缺少了dif的两个类别。阵列。请参阅我的更新代码....此外,我可以使用任何JS方法使其工作? – missgg

+0

你的目标对我来说还不清楚。你是否试图根据用户从'$ scope.mySelection'中选择的内容过滤$ scope.mSessions和$ scope.cSessions的显示?或者,你是否试图根据其他数组的内容为'$ scope.mySelection'生成项目?如果是后者,则需要将'm_category'或'category'关联到其各自的'$ scope.mySelection'项目的某种方式(例如公共连接键)。顺便说一下,这些方法根本不是特定于Java--它们在函数式编程中很常见。 – miqid

回答

0

如果要筛选都基于选择的表,然后尝试下面的代码:

工作小提琴:https://jsfiddle.net/almamun1996/5Ln0d5Lb/14/

HTML:

<div ng-app="myFruit" ng-controller="fruitController"> 
<select ng-model="selectedFruit" ng-options="c.cat for c in mySelection" ng-change="myDropdownChange(selectedFruit)"></select> <br/> 
Fruit Seleted: {{selectedFruit.cat}} 
<table> 
    <tr> 
    <td>Session Name</td> 
    <td>M Category</td>  
    </tr> 
    <tr ng-repeat="m in mSessions">   
    <td>{{m.name}}</td> 
    <td>{{m.m_category}}</td>   
    </tr> 
</table> 
<table> 
    <tr> 
    <td>C Category</td> 
    </tr> 
    <tr ng-repeat="c in cSessions ">   
    <td>{{c.category}}</td>   
    </tr> 
</table> 

JS(控制器):

$scope.mySelection = [ 
       {"cat": "RED FRUIT", "m_category": ["Apple", "Strawberry", "Pineapple"], "category": [{"RED": ["YES"]}]}, 
       {"cat": "YELLOW FRUIT", "m_category": ["Banana", "Pineapple"], "category": [{"YELLOW": ["YES"]}]}, 
       {"cat": "ORANGE FRUIT", "m_category": ["Peach", "Nectarine"], "category": [{"ORANGE": ["YES"]}]} 
      ]; 
     $scope.mSessions = [{ 
      "id": 2, 
      "name": "BD20", 
      "m_category": ["Apple", "Strawberry", "Pineapple"] 
      }, { 
      "id": 3, 
      "name": "CS03", 
      "m_category": ["Banana", "Pineapple"] 
      }, { 
      "id": 4, 
      "name": "MIS99", 
      "m_category": ["Peach", "Nectarine"] 
      } 
    ]; 

     $scope.cSessions = [{ 
      "number": 439, 
      "name": "FDFG", 
      "category": [{"RED": ["YES"]}] 
      }, { 
      "number": 34, 
      "name": "CN", 
      "category": [{"YELLOW": ["YES"]}] 
      }, { 
      "number": 44, 
      "name": "PPP", 
      "category": [{"ORANGE": ["YES"]}] 
      } 
    ]; 

    let m_myArray = $scope.mSessions; 
    let c_myArray = $scope.cSessions; 
    $scope.myDropdownChange = function(fruitSelected){ 
     let m_myArray_inner = []; 
     let c_myArray_inner = []; 
     angular.forEach(m_myArray, function(value, key){ 
      if(areArraysEqual(fruitSelected.m_category, value.m_category)){ 
       m_myArray_inner = [{'name': value.name, 'm_category': value.m_category}] 
      } 
     }) 
     angular.forEach(c_myArray, function(value, key){ 
      if(areArraysEqual(fruitSelected.category, value.category)){ 
       c_myArray_inner = [{'category': value.category}]; 
      } 
     }) 
     $scope.mSessions = m_myArray_inner 
     $scope.cSessions = c_myArray_inner; 
    } 

    function areArraysEqual(x, y) { 
     // If both x and y are null or undefined and exactly the same 
     if (x === y) { 
      return true; 
     } 
     // If they are not strictly equal, they both need to be Objects 
     if (! (x instanceof Object) || ! (y instanceof Object)) { 
      return false; 
     } 
     // They must have the exact same prototype chain, the closest we can do is 
     // test the constructor. 
     if (x.constructor !== y.constructor) { 
      return false; 
     } 
     for (var p in x) { 
      // Inherited properties were tested using x.constructor === y.constructor 
      if (x.hasOwnProperty(p)) { 
       // Allows comparing x[ p ] and y[ p ] when set to undefined 
       if (! y.hasOwnProperty(p)) { 
        return false; 
       } 
       // If they have the same strict value or identity then they are equal 
       if (x[ p ] === y[ p ]) { 
        continue; 
       } 
       // Numbers, Strings, Functions, Booleans must be strictly equal 
       if (typeof(x[ p ]) !== "object") { 
        return false; 
       } 
       // Objects and Arrays must be tested recursively 
       if (!areArraysEqual(x[ p ], y[ p ])) { 
        return false; 
       } 
      } 
     } 
     for (p in y) { 
      // allows x[ p ] to be set to undefined 
      if (y.hasOwnProperty(p) && ! x.hasOwnProperty(p)) { 
       return false; 
      } 
     } 
     return true; 
    } 
+0

感谢彻底的方法来做字符串比较!我确实使用了一些.hasOwnProperty和indexOf来进行字符串比较。我喜欢你涵盖了字符串比较工作的所有场景,我可以将其用于另一个项目。另外,'let'的使用非常酷。以前从未使用过。 – missgg