javascript - ng-repeat 包含具有相似键值的内部数组的数据的数组会分组在一起吗?

标签 javascript angularjs

我有一个数据数组,其中包含另一个数据数组,其键(TypeOfServiceAssigned:Array(2))在两个内部数组中都是通用的,我想以类似的键值组合在一起的方式应用ng-repeat,并且不同的一组。

为了更好的解释,请参见下面:

Array = [
  {
    Quotation:1200,
    TypeOfServiceAssigned:["Primary", "Partial Services"],
    VendorEmail:"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fabbba9d979b9396d4999597" rel="noreferrer noopener nofollow">[email protected]</a>"
  },{
    Quotation:1350,
    TypeOfServiceAssigned:["Partial Services"],
    VendorEmail:"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="78391c1c381f15191114561b1715" rel="noreferrer noopener nofollow">[email protected]</a>"
  }
];

我想在数组上应用 ng 重复,以便它根据 TypeOfServiceAssigned 值向我显示两组,一组用于主要服务,第二组用于部分服务。对于部分服务商,它会显示两个列表。

最佳答案

最有效的方法是手工制作函数以返回您想要的确切内容。 example here ,然后像平常一样将其绑定(bind)到嵌套的 ng-repeat :)

但是通常我们应该在服务器端/服务上执行此操作,但无论如何......

function doGroupBy() {
    var result = {};
    for (var i = 0; i < input.length; ++i) {
        var current = input[i].TypeOfServiceAssigned;
        for (var j = 0; j < current.length; ++j)
            if (result[current[j]])
                result[current[j]].push(input[i]); // result[current[j]] = [...result[current[j]], input[i]];
            else
                result[current[j]] = [input[i]];
    }

    return result;
}

var input = [{ Quotation: 1200, TypeOfServiceAssigned: ["Primary", "Partial Services"], VendorEmail: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="733233141e121a1f5d101c1e" rel="noreferrer noopener nofollow">[email protected]</a>" },
             { Quotation: 1350, TypeOfServiceAssigned: ["Partial Services"], VendorEmail: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f0e2b2b0f28222e2623612c2022" rel="noreferrer noopener nofollow">[email protected]</a>" }];

var testResult = doGroupBy(input);

for (var key in testResult) {
    console.log('-----------');
    console.log(key);
    console.log(testResult[key]);
}

关于javascript - ng-repeat 包含具有相似键值的内部数组的数据的数组会分组在一起吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44392457/

相关文章:

javascript - For 循环出现不足

javascript - 是否可以使用 dropzone.js 获取文件上传的文件夹名称

javascript - AngularJS 中的 Textarea 不渲染 HTML

angularjs - 使用 Passportjs 从 Angularjs ( http get/resource ) 进行用户身份验证

javascript - 从 AngularJS 中的子指令获取对 Controller 的访问权限

javascript - 未捕获的类型错误 : Cannot read property 'fname' of undefined at FillInfo()

javascript - JSON 到客户端的 ASP.NET 脚本服务的字符串?

javascript - angularjs选择框默认选择基于数据库id

javascript - AngularJS 在数组中循环多个数组

angularjs - 如何在AngularJS的日期选择器文本框中设置今天的日期