0

I have a select - option in angular, and I need to check values that have same id as id in database, so I have tried something like this:

isDropdownValueSelected(amf: ApplicationModuleForm): boolean {
    for (let i = 0; i < this.role.applicationForms.length; i++) {
      if (this.role.applicationForms[i].id == amf.id) {
        return true;
      }
      else {
        return false;
      }
    }
 }

My angular part:

<div class="col-lg-9">
     <select id="applicationModuleFormSelect" name="applicationModuleFormSelect" class="form-control multiselect-select-all" multiple="multiple" data-fouc>
        <option *ngFor="let amf of appModuleForms;" [value]="amf.id" [selected]="isDropdownValueSelected(amf)">{{amf.title}}</option>
     </select>
</div>

So basically there I wanted to loop for each id in option and if I found similar in my array I would return true, because array this.role.applicationForms holds values from database, but unfortunatelly this does not works, nothing is selected in dropdown and I tested with console log it says only 1 value exist even if there are 3..

Thanks guys Cheers

1

5 Answers 5

1

Maybe you need to move the false value to the end for returning, because every return statement ends the function.

isDropdownValueSelected(amf: ApplicationModuleForm): boolean {
    for (let i = 0; i < this.role.applicationForms.length; i++) {
        if (this.role.applicationForms[i].id == amf.id) {
            return true;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

1

This function only works when the id of the first element is matched, because you're returning the value on every check.

You should update the code to be like this:

isDropdownValueSelected(amf: ApplicationModuleForm): boolean {
    for (let i = 0; i < this.role.applicationForms.length; i++) {
        if (this.role.applicationForms[i].id == amf.id) {
            return true;
        }
    }
    return false;
}

Comments

0

Assuming applicationForms is an array, you can use the method some:

isDropdownValueSelected(amf: ApplicationModuleForm): boolean {
  return this.role.applicationForms.some(({id}) => id === amf.id);
}

Comments

0

Use the some operator instead of looping on your elements:

isDropdownValueSelected(amf: ApplicationModuleForm): boolean {
    return this.role.applicationForms.some(e => e.id === amf.id);
}

Comments

0

You do not need for loop:

isDropdownValueSelected(amf: ApplicationModuleForm): boolean {
            if (this.role.applicationForms.find(x => x.id == amf.id)) {
                return true;
            }
            else {
                return false;
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.