skip to Main Content

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.

  1. If all object status is new and remainingTime > 0, than isButtonEnable Flag
    is True.
  2. If any 2 object is submitted status and third object is New
    status and remainingTime > 0 than, isButtonEnable flag True.
  3. If any 1 object is submitted status, and other 2 object is New status and remainingTime < 0, than, isButtonEnable flag False.
  4. If any 1 object is submitted status,and other 2 object is New status and remainingTime > 0 than, isButtonEnable flag True.

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;
        }        
      });
    }

  }

2

Answers


  1. Try with this

    function checkButtonStatus(data) {
        let newCount = 0;
        let submittedCount = 0;
        let remainingTimePositive = true;
    
        for (let item of data) {
            if (item.status === "New") {
                newCount++;
            } else if (item.status === "Submitted") {
                submittedCount++;
            }
    
            if (item.remainingTime <= 0) {
                remainingTimePositive = false;
            }
        }
    
        let isButtonEnable = false;
    
        if (newCount === data.length && remainingTimePositive) {
            isButtonEnable = true;
        } else if (submittedCount === 2 && newCount === 1 && remainingTimePositive) {
            isButtonEnable = true;
        } else if (submittedCount === 1 && newCount === 2 && !remainingTimePositive) {
            isButtonEnable = false;
        } else if (submittedCount === 1 && newCount === 2 && remainingTimePositive) {
            isButtonEnable = true;
        }
    
        return isButtonEnable;
    }
    
    Login or Signup to reply.
  2. Here is a code based on your condition.

    enableFlagOnStatus() {
      let newCount = 0;
      let submittedCount = 0;
      let remainingTime = 0;
    
      this.data.forEach((item) => {
        if (item.status === 'New') {
          newCount++;
          remainingTime = item.remainingTime;      
        } else if (item.status === 'Submitted') {
          submittedCount++;
        }
      });
    
      if (newCount === this.data.length && 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;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search