Check out the demo on stackblitz: here
The scenario is this: the server responds in a specific format, and based on certain conditions, we need to determine whether to show or hide buttons on a page. Each button has its own click function, which is why the buttons are statically declared on the page.
We have an array of objects with properties that need to be mapped to other properties under certain conditions.
collections = [
{
"productId": "samsung",
"productParams": "",
"isAvailable": true
},
{
"productId": "nokia",
"productParams": "",
"isAvailable": true
},
{
"productId": "Lg",
"productParams": "",
"isAvailable": false
},
]
Within this collection array, I am attempting to map the object's properties based on two conditions:
If the value of productId
matches a specific string and the isAvailable
property is set to true
, I assign it to a global variable to show the respective button. However, there seems to be an issue with the code logic. Can someone help me identify what I did wrong?
getClick() {
let showButtonSamsung, showButtonNokia, showButtonLg;
let x = this.collections.map(x => {
showButtonSamsung = x.productId == 'samsung' && x.isAvailable == true ? true : false;
showButtonNokia = x.productId =='nokia' && x.isAvailable == true ? true : false;
showButtonLg = x.productId == 'Lg' && x.isAvailable == true ? true : false;
});
}
Expected Output:
showButtonSamsung: true // will show the button
showButtonNokia: true // will show the button
showButtonLg: false // hide the button