angular2中的组数组

时间:2017-06-14 06:09:57

标签: angular typescript ngfor

我有像这样的json响应

JSON响应

"Terms": [
            {
                "Help": "Terms",
                "EventType": "Success",
                "Srno": 1,
                "Heading": "Discount Condition",
                "TermsDesc": "50 % Discount on 1st Cab Ride"
            },
            {
                "Help": "Terms",
                "EventType": "Success",
                "Srno": 2,
                "Heading": "Discount Condition",
                "TermsDesc": "20% Discount on Nights more than 8"
            },
            {
                "Help": "Terms",
                "EventType": "Success",
                "Srno": 1,
                "Heading": "Stay Condition",
                "TermsDesc": "No More than 10% Discount on Less than 4 Nights Stay"
            },
            {
                "Help": "Terms",
                "EventType": "Success",
                "Srno": 2,
                "Heading": "Stay Condition",
                "TermsDesc": "20% Discount on Nights more than 8"
            },
            {
                "Help": "Terms",
                "EventType": "Success",
                "Srno": 3,
                "Heading": "Discount Condition",
                "TermsDesc": "No More than 10% Discount on Less than 4 Nights Stay"
            }
        ],

我希望按Heading对其进行分组。类似的Heading应该有TermsDesc例如。来自回应的折扣条件保持条件

折扣条件

  • “第1次出租车50%折扣”
  • “超过8晚的20%折扣”
  • “condition 3”

保持条件

  • “第1次出租车50%折扣”
  • “超过8晚的20%折扣”
  • “condition 3”

我得到回应的组件功能

 getTerms(){
    this.quoteService.getTerms()
    .subscribe(
      terms => {
        this.terms = terms.resultData.Terms;
        this.notes = terms.resultData.ImpNotes;
        this.exclusions = terms.resultData.Exclusions;
        var arr=[];
        var strHeading;
        var title;
        var temp = 0;
        var listing = [];
        for(let i=0;i<this.terms.length;i++) {
          strHeading=this.terms[i].Heading;
          title=this.terms[i].TermsAndConditionsDesc;
          if(!this.uniqTerm[temp]){
            this.uniqTerm[temp] = [];
          }
          if (arr.indexOf(strHeading) !== -1) {
            this.uniqTerm[temp].push({"header":strHeading});
          } 
          else {
               arr.push(strHeading);               
               this.uniqTerm[temp].push({"header":strHeading});               
               listing.push({"title":title});
               this.uniqTerm[temp].push({"title":title});
               temp++;                 
          }
      },
      response => {
        if (response.status == 404) {
        }
      });
  }

如何创建数组,以便我可以在*ngFor

中使用它

我想我需要像

这样的东西
[0]:object
     Heading:"Discount Condition"
     TermsDesc: object
                term: Term1 
                term: Term2

[1]:object
     Heading:"Stay Condition"
     TermsDesc: object
                term: Term1 
                term: Term2

但不知道如何在angular2中实现这一点 请帮助我,如果有人有更好的建议,我很乐意尝试

由于

2 个答案:

答案 0 :(得分:3)

您可以这样使用linq-es2015库:

import { asEnumerable } from 'linq-es2015';

class Term
{
    public Help: string;
    public EventType: string;
    public Srno: number;
    public Heading: string;
    public TermsDesc: string;
}

//somewhere in component
this.terms: Term[];
this.result = asEnumerable(this.terms).GroupBy(x => x.Heading, x => x, (key, b) => { return { Heading: key, TermsDesc: asEnumerable(b).ToArray() } }).ToArray();

//template
<ul>
   <li *ngFor="let term of result">
       {{term.Heading}}
       <ul>
           <li *ngFor="let item of term.TermsDesc">{{item.TermsDesc}}</li>
       </ul>
   </li>
</ul>

答案 1 :(得分:1)

经过一番挖掘后得到了解决方案,Slava Utesinov的答案仍然有效,但需要安装linq-es2015

const groupedObj = this.terms.reduce((prev, cur)=> {
    if(!prev[cur["Heading"]]) {
        prev[cur["Heading"]] = [cur];
    } else {
        prev[cur["Heading"]].push(cur);
    }
    return prev;
}, {});
this.TermCons = Object.keys(groupedObj)
.map(Heading => ({ Heading, TermsDesc: groupedObj[Heading] }));