I am working with different array lengths stored in variables and trying to determine which variable is the greatest, then change the font color of that variable. However, I encountered an issue where if two variables have the same value, only one is being colored.
Here is my code snippet:
calculateconfirm(location1, location2, location3, location4, type) {
var max = Number.NEGATIVE_INFINITY;
var max_key = undefined;
var obj = { 'location1': location1, 'location2': location2, 'location3': location3, 'location4': location4 };
for (var key in obj) {
if (obj[key] > max) {
max_key = key;
max = obj[key];
}
}
return max_key == type ? '#5D3FD3' : '#301934';
}
HTML
<tr *ngFor="let x of userdata">
<td><img src="{{x.data.userImage}}" alt="" style="height: 75px; width: 75px;"></td>
<td>{{x.data.fullName}}</td>
<td *ngIf="x.data.location.location1 == 'false'">N/A</td>
<td *ngIf="x.data.location.location1 != 'false'"
[style.color]="calculateconfirm(x.data.location.like.length, x.data.location2.like.length, x.data.location3.like.length, x.data.location4.like.length, 'location1')">
{{x.data.location.location1}}
<td *ngIf="x.data.location2.location2 == 'false'">N/A</td>
<td *ngIf="x.data.location2.location2 != 'false'"
[style.color]="calculateconfirm(x.data.location.like.length, x.data.location2.like.length, x.data.location3.like.length, x.data.location4.like.length, 'location2')">
{{x.data.location2.location2}}
<td *ngIf="x.data.location3.location3 == 'false'">N/A</td>
<td *ngIf="x.data.location3.location3 != 'false'"
[style.color]="calculateconfirm(x.data.location.like.length, x.data.location2.like.length, x.data.location3.like.length, x.data.location4.like.length, 'location3')">
{{x.data.location3.location3}}
<td *ngIf="x.data.location4.location4 == 'false'">N/A</td>
<td *ngIf="x.data.location4.location4 != 'false'"
[style.color]="calculateconfirm(x.data.location.like.length, x.data.location2.like.length, x.data.location3.like.length, x.data.location4.like.length, 'location4')">
{{x.data.location4.location4}}
</td>
</tr>
Let's assume we have an object like this:
var obj = { 'location1': 2, 'location2': 1, 'location3': 5, 'location4': 5 };
In this case, I want both location 3 and 5 to be colored on the HTML page.