I am getting below array of object from api response, i wanted to create one function which manipulate the data and enable and disable the flag based on below condition.
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
}
];
Based on below cases, we need to enable/Disbale flag inside code.
Can anyone help me to check this condition and enable/Disable the flags.
Below is my code which i Tried
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;
}
});
}
}
Here is a code based on your condition.
enableFlagOnStatus() {
if(data.length > 1){
let newCount = 0;
let submittedCount = 0;
let remainingTime = 0;
this.data.forEach((item) => {
if (item.status === 'New') {
newCount++;
} else if (item.status === 'Submitted') {
submittedCount++;
}
remainingTime = item.remainingTime;
});
if (newCount === 3 && remainingTime > 0) {
this.isButtonEnabled = true;
} else if (submittedCount >= 2 && newCount === 1 && remainingTime > 0) {
this.isButtonEnabled = true;
} else if (submittedCount === 1 && newCount === 2 && remainingTime < 0) {
this.isButtonEnabled = false;
} else if (submittedCount === 1 && newCount === 2 && remainingTime > 0) {
this.isButtonEnabled = true;
} else {
this.isButtonEnabled = false;
}
}
}
Note: Please also correct the spelling of "isButtonEnale" to "isButtonEnabled".