How to manipulate Array of Object based on condition Angular 8

46 Views Asked by At

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

There are 2 best solutions below

0
Jignesh Panchal On BEST ANSWER

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".

0
Den On

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