I am working with an observable called myObservable$ that provides a specific array of objects:
[
{ "firstProp": "A", "secondProp": "NA", "available": false },
{ "firstProp": "B", "secondProp": "NA", "available": true },
{ "firstProp": "C", "secondProp": "Old", "available": false },
{ "firstProp": "C", "secondProp": "New", "available": false }
]
My objective is to create a new array by filtering based on 'firstProp' and 'available', and then pushing into a new structure:
[
{ imgSrc: 'pathTo/myImages/file1.svg', badgeStyle: val?.available ? 'fas fa-exclamation-circle fa-lg' : 'fas fa-check-circle fa-lg', cardLabel: 'First Label'},
{ imgSrc: 'pathTo/myImages/file2.svg', badgeStyle: val?.available ? 'fas fa-exclamation-circle fa-lg' : 'fas fa-check-circle fa-lg', cardLabel: 'Second Label'},
{ imgSrc: 'pathTo/myImages/file3.svg', badgeStyle: val?.available ? 'fas fa-exclamation-circle fa-lg' : 'fas fa-check-circle fa-lg', cardLabel: 'Third Label'},
]
The current solution works, but I believe there may be room for improvement:
const OBJ1 = { imgSrc: 'pathTo/myImages/file1.svg', badgeStyle: val?.available ? 'fas fa-exclamation-circle fa-lg' : 'fas fa-check-circle fa-lg', cardLabel: 'First Label'};
const OBJ2 = { imgSrc: 'pathTo/myImages/file2.svg', badgeStyle: val?.available ? 'fas fa-exclamation-circle fa-lg' : 'fas fa-check-circle fa-lg', cardLabel: 'Second Label'};
const OBJ3 = { imgSrc: 'pathTo/myImages/file3.svg', badgeStyle: val?.available ? 'fas fa-exclamation-circle fa-lg' : 'fas fa-check-circle fa-lg', cardLabel: 'Third Label'};
myObservable$.subscribe((value) => {
value?.forEach((val) => {
if (val?.firstProp === 'A') {
this.myNewArray.push(OBJ1);
} else if (val?.firstProp === 'B') {
this.myNewArray.push(OBJ2);
} else if (val?.firstProp === 'C' && val?.secondProp === 'New') {
this.myNewArray.push(OBJ3);
}
});
});
I am facing difficulties in handling the scenario where:
as I encounter two instances of 'firstProp' being 'C' with different types of 'secondProp' (Old and New). The challenge is to display 'exclamation' in badgeStyle when either one or both 'C' have "available": true, and to show a 'green-tick' for badgeStyle when both 'C' have "available": false.