双环得到吴选项 [英] Double loop to get Ng-options

查看:182
本文介绍了双环得到吴选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的所有任务名称作为任务ID,其中任务的阵列存储在组阵列。
我试图得到它angularJS的NG选项指令。这里是 http://jsfiddle.net/753YB/16/ 链接快速编辑。
提前致谢!我知道在单个阵列是 task.Id作为task.Name在任务,但我的任务中嵌套组,所以我需要的所有组中的任务任务。

  $ scope.Groups = {[
        产品名称:A1,
        ID:1,
       任务:[{名称:有些名称ID:1},{名称:有些名称2ID:2}]
    },{
        产品名称:C2,
        编号:2,
        任务:[{名称:有些名称3'ID:3},{名称:有些名称4'ID:4}]
    },
       {
        产品名称:C2,
        ID:3,
        任务:[{名称:有些名称3'ID:5},{名称:有些名称4'ID:6}]
    }];


解决方案

您可以用下面的函数扁平化你的数据结构:

 函数flattenArray(数组,FN){
   VAR输出= [];
   对于(VAR I = 0; I< array.length ++我){
      VAR的结果= FN(数组[我]);
      如果(结果)
         输出= output.concat(结果);
   }
   返回输出;
}

通过传递一个数组和一个项目的回调调用该函数。回调返回子阵列。

  $ scope.Groups = flattenArray(组,函数(项目){
     返回item.Tasks;
});

演示小提琴

I want to get all the tasks name as task id, where array of tasks are stored in array of groups. I am trying to get it in ng-options directive of angularJS. Here is http://jsfiddle.net/753YB/16/ link for quick edit. thanks in advance! I know in single array it is task.Id as task.Name for task in Tasks but my tasks are nested in groups so I need tasks in all group.

$scope.Groups = [{
        Name: 'a1',
        Id: 1,
       Tasks:[{Name: 'Some Name' Id: 1},{Name: 'Some Name 2' Id: 2}]
    }, {
        Name: 'c2',
        Id: 2,
        Tasks:[{Name: 'Some Name 3' Id: 3},{Name: 'Some Name 4' Id: 4}]
    },
       {
        Name: 'c2',
        Id: 3,
        Tasks:[{Name: 'Some Name 3' Id: 5},{Name: 'Some Name 4' Id: 6}]
    }];

解决方案

You can flatten your data structure with the following function:

function flattenArray(array, fn)  {
   var output = [];
   for(var i = 0; i < array.length; ++i) {
      var result = fn(array[i]);
      if (result) 
         output = output.concat(result);
   }
   return output;
}

Call the function by passing it an array and an item callback. The callback returns the sub-array.

$scope.Groups = flattenArray(groups, function(item) { 
     return item.Tasks;
}); 

Demo Fiddle

这篇关于双环得到吴选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆