I have received an array of objects from an API response and I need to create a function that manipulates the data by enabling or disabling a flag based on certain conditions.
API Response
const data = [
{
"subfamily": "Hair Liquids (Includes: Bottles & Tottles, Tubes, Jars & Pouches)",
"status": "New",
"remainingTime": 6,
},
{
"subfamily": "Skin Care - Liquids (Includes: Bottles & Tottles, Tubes, Jars & Pouches)",
"status": "Submitted",
"remainingTime": 6
},
{
"subfamily": "Styling",
"status": "New",
"remainingTime": 6
}
];
In order to determine whether the isButtonEnable flag should be enabled or disabled, we need to consider the following cases:
- If all object statuses are "New" and remainingTime is greater than 0, then set the isButtonEnable flag to True.
- If exactly 2 objects have a status of "Submitted" and one object has a status of "New" with remainingTime greater than 0, then set the isButtonEnable flag to True.
- If exactly 1 object has a status of "Submitted" and the other 2 objects have a status of "New" with remainingTime less than 0, then set the isButtonEnable flag to False.
- If exactly 1 object has a status of "Submitted" and the other 2 objects have a status of "New" with remainingTime greater than 0, then set the isButtonEnable flag to True.
I am looking for assistance in checking these conditions and updating the flags accordingly.
Below is the code snippet I have attempted:
enableFlagOnStatus(){
if(data.length > 1){
data.forEach((currentValue, index) => {
if(currentValue.status === 'New' && remainingTime > 0){
this.isButtonEnale = true;
} else if(currentValue.status === 'Submitted' && remainingTime < 0){
this.isButtonEnale = false;
}
});
}
}