简体   繁体   English

如何检查javascript数组是否持有具有特定值的属性,如果是,则返回true

[英]How to check if javascript array holds property with specific value, if so returns true

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: 我在角度中有一个select-选项,我需要检查与数据库中的id具有相同id的值,因此我尝试了以下操作:

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.. 因此,基本上,我想为选项中的每个id循环,如果我在数组中发现相似的对象,我将返回true,因为数组this.role.applicationForms保留了数据库中的值,但不幸的是,这不起作用,下拉菜单中未选择任何内容,我测试了控制台日志,它说即使存在3,也只有1个值。

Thanks guys Cheers 谢谢大家的欢呼

Maybe you need to move the false value to the end for returning, because every return statement ends the function. 也许您需要将false值移到末尾才能返回,因为每个return语句都会终止该函数。

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

This function only works when the id of the first element is matched, because you're returning the value on every check. 仅当第一个元素的ID匹配时,此函数才起作用,因为您每次检查都会返回该值。

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

Assuming applicationForms is an array, you can use the method some : 假设applicationForms是一个数组,则可以使用一些方法:

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

Use the some operator instead of looping on your elements: 使用some运算符而不是在元素上循环:

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

You do not need for loop: 你不需要for循环:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查某个值是否存在于将对象作为其元素的数组中。 JavaScript - How to check if a certain value exist in an array which holds objects as its elements. JavaScript JavaScript-检查属性的值是否为true并返回字符串 - JavaScript - check if the value of the property is true and return string 如何根据持有的属性值拆分数组的条目? - How to split entries of an array based on what the property value holds? 如何使用JavaScript删除包含特定值的大字符串中的模式? - how to remove a pattern in big string that holds specific value using javascript? 如何检查 mongodb.find 是否返回一些东西,或者不是那么真或假 - How to just check if the mongodb .find returns something or not so true or false 检查对象数组的特定属性值 - Check array of objects for a specific property value 检查数组是否包含具有特定属性值的对象 - Check if array contains an object with the value of a specific property 如何检查Javascript数组中是否存在属性值? - How to check if a property value exists within a Javascript array? 如何检查两个数组中的属性值 object 值 javascript - how to check property value in two array of object values javascript 如何 select 多个特定属性与 javascript 数组中的不同值 - How to select multiple specific property with distinct value from javascript array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM