I have to handle a JSON file.
In JSON BLOCAGES -> BLOCAGE > I have to retrieve QTE
and DTE
.
Here is my file JSON here
My problem is that, I display QTE
and DTE
one time.
For now, I have this => enter image description here
I want to get this result => enter image description here
I’m missing the array elements [1]
and [2]
, how could I add them from a loop?
Here is my method prepareDataForBlocage()
JavaScript
x
16
16
1
prepareDataForBlocage(response) {
2
this.spinners.blockDetails = false;
3
if (response['RETURNCODE'] == "OKK00") {
4
5
6
let [variable1] = response['BLOCAGES']['BLOCAGE'];
7
8
this.statusLine = {
9
quantity: variable1['QTE'],
10
update: variable1['DAT'],
11
12
};
13
14
}
15
}
16
My HTML
JavaScript
1
20
20
1
<table class="table table-striped">
2
<tr style="background-color: #f8f9fa; font-weight: bold;">
3
<td style="width: 13%;">Quantity</td>
4
<td style="width: 13%;">Reason</td>
5
<td style="width: 13%;">Date</td>
6
<td style="width: 13%;">Référence</td>
7
</tr>
8
<tr *ngIf="statusLine.quantity != 0" >
9
<td>
10
{{statusLine.quantity | number:'1.2-2' }}
11
</td>
12
<td></td>
13
<td>
14
{{statusLine.update | dateddmmyyyy }}
15
</td>
16
<td>
17
</td>
18
</tr>
19
</table>
20
Advertisement
Answer
I see 2 problems in your code
the first:
JavaScript
1
5
1
let [variable1] = response['BLOCAGES']['BLOCAGE'];
2
/// is the same as
3
let variable1 = response['BLOCAGES']['BLOCAGE'][0];
4
/// That means you only get the first element of your list
5
the second is that you do not loop over an array in your template.
You could try something like:
JavaScript
1
7
1
this.myList = response['BLOCAGES']['BLOCAGE']
2
.filter(blocage => blocage['QTE'] != 0)
3
.map(blocage => ({
4
quantity: blocage['QTE'],
5
update: blocage['DAT'],
6
}));
7
JavaScript
1
12
12
1
<tr *ngFor="let elem of myList" >
2
<td>
3
{{elem.quantity | number:'1.2-2' }}
4
</td>
5
<td></td>
6
<td>
7
{{elem.update | dateddmmyyyy }}
8
</td>
9
<td>
10
</td>
11
</tr>
12