Received a JSON response structured like this
JSON response
"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"
}
],
The goal is to categorize the terms by Heading
. Terms with the same Heading
should be grouped together, for example: Discount Condition and Stay Condition from the original response
Discount Condition
- "50 % Discount on 1st Cab Ride"
- "20% Discount on Nights more than 8"
- "condition 3"
Stay Condition
- "50 % Discount on 1st Cab Ride"
- "20% Discount on Nights more than 8"
- "condition 3"
The function component where the response is processed:
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) {
}
});
}
How can I format an Array that can be used in *ngFor
?
I assume it would need to look something like this
[0]:object
Heading:"Discount Condition"
TermsDesc: object
term: Term1
term: Term2
[1]:object
Heading:"Stay Condition"
TermsDesc: object
term: Term1
term: Term2
However, I am unsure of how to achieve this using Angular 2. Any assistance or alternative suggestions would be greatly appreciated.
Thank you