简体   繁体   English

如何使用 Array includes() 方法检查数组中是否存在值?

[英]How can i use Array includes() Method to check value exist in array?

I have the following code which get all the data in kendo grid column Type .我有以下代码可以获取剑道网格列Type所有数据。 how can I check if array contain Type ='Custom Document' ?如何检查数组是否包含Type ='Custom Document'

I tried the following我尝试了以下

function chkCustomDocumentIncluded() {
    var messageText = '';

    var arrayType = [];
    var data = $("#CustomDocumentsGrid").data("kendoGrid").dataSource._data;
    for (i = 0; i < data.length; i++) {
        arrayType.push(data[i].Type);          
    }       
    if (arrayType.includes("Custom SBA")) {
        messageText = '@Localizer["Custom document is required"].Value';
    }

    return messageText;
}

console.log(arrayType) returns expected value but how can i add a condition to check the value exist? console.log(arrayType)返回预期值,但如何添加条件来检查值是否存在?

Use the Array.every method to check if every element of the array meets a condition.使用Array.every方法检查数组的每个元素是否都满足条件。

function chkCustomDocumentIncluded() {
    var data = $("#CustomDocumentsGrid").data("kendoGrid").dataSource._data;
    if (!data || data.every(d => d.Type == "Custom SBA")) {
        return '@Localizer["Custom document is required"].Value';
    } else {
        return '';
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM