检查数组Javascript中是否存在一组值 [英] Check if a group of values exist in array Javascript
问题描述
我有一个像这样的表,其中有一些location值.如果此位置值包含除"California" "New york" ,"HQ" , "Florida"
以外的任何其他值,则应显示警告消息,例如location无效.
我已经尝试过以下代码.
I have a table like this in which i have some values for location . If this location values contains any other values except "California" "New york" ,"HQ" , "Florida"
it should show alert message like location is not valid .
I have tried following code .
tabledata.DataTable.forEach(function (data) {
var locate = data.Location;
if (locate.includes(data.BranchName)) {
alert("Success");
} else {
alert("failure");
}
})
更新
branchDetails.forEach(function (data) {
console.log(data.BranchName);
});
此处data.BranchName
包含"California" "New york" ,"HQ" , "Florida"
,而locate
包含表位置值.
here data.BranchName
contains "California" "New york" ,"HQ" , "Florida"
and locate
contains the table location value .
我的问题是,即使表包含"HQ"
,它也显示为失败
My issue here is even though the table contains "HQ"
it is showing as failure
推荐答案
我认为您刚刚使用的方法有误.尝试使用:
I think you just used includes the wrong way. Try using:
data.Branchname.includes(locate)
编辑
您还可以安全地检查情况数据.分支名称未定义
You could also safe check in case data.Branchname is undefined
data.Branchname && data.Branchname.includes(locate)
这篇关于检查数组Javascript中是否存在一组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!