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

Artem Arkhipov
  • 6,207
  • 5
  • 30
  • 51
Roxy'Pro
  • 3,742
  • 6
  • 36
  • 82
  • related: https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript – tanaydin Feb 05 '19 at 10:40

5 Answers5

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;
}
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
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;
}
Ryan Walls
  • 181
  • 1
  • 11
Ankur Jain
  • 221
  • 1
  • 6
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);
}
ZER0
  • 23,634
  • 5
  • 48
  • 53
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);
}
jo_va
  • 12,701
  • 3
  • 23
  • 44
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;
            }
        }
Joe Belladonna
  • 1,301
  • 14
  • 20