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
  • 7,025
  • 5
  • 30
  • 52
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • 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
  • 376,160
  • 25
  • 347
  • 392
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
  • 191
  • 1
  • 13
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
  • 24,846
  • 5
  • 51
  • 54
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
  • 13,504
  • 3
  • 23
  • 47
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,349
  • 14
  • 20