0

I have a tree data whose interface is defined as below:

interface IData {
  id: string;
  label: string; .
  value: string;
  expanded: boolean;
  condition?: boolean;
  dataState: 'SUCCESS';
  locked: boolean;
  enabled: boolean;
  parent: string;
  children: IData[];
}

And I have a list of errors with the following interface :-

interface IErrors {
  id: string;
  success: boolean;
  errors: Array<IError>;
}

I would like to iterate over these errors and apply 'dataState' to 'FAILED' if any error.id is found on the IData[] i.e. I would have to change the node's 'dataState' property to 'FAILURE' if the data id matches with that in the list of error id's.

I've written the following logic, but it's taking 2 seconds for it to execute ( check with console.time ).

  public setFailedDataToStatusFailed(data:IData[],errorsList:IErrors[]){
    data.forEach(data => {
      errorsList.forEach(error => {
        if(data){
          if(data.id === error.id){
            data.ruleState = 'FAILURE';
          return;
          } else if(data.children){
            this.setFailedRulesToStatusFailed(data.children,errorsList);
          }
        }
      });
    });
  }

Is there a way to optimize this logic better ?

Thanks!

Veryon890
  • 330
  • 1
  • 4
  • 15

1 Answers1

0
data.map(d =>   {
      if(errorsList.findIndex( err => err.id === d.id )){
            data.ruleState = 'FAILURE';
            return;
      }
      if(d.children){
         this.setFailedRulesToStatusFailed(data.children,errorsList);
      }
    })
Moufeed Juboqji
  • 690
  • 4
  • 11
  • this logic is not changing the 'ruleState' of the children and it's children. It is exiting at the parent. – Veryon890 Jul 21 '20 at 08:38