我有一个数据数组,其中包含另一个数据数组,其键(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/