检查另一个

时间:2018-03-06 04:40:34

标签: javascript jquery arrays if-statement

我正在努力处理下面的一段代码,并且无法在堆栈上的任何地方找到类似的东西。

我尝试过使用JavaScript .indexof(返回所有不包含变量的内容)以及.match(显示所有内容,即使包含单词的一部分。我需要它才是确切的原因)和甚至jquery。

我想知道是否有人可以帮助我。

我有一个脚本可以将数据从服务器提取到以下数组中。

从那里我需要能够将此数组与页面上的数据进行比较。如果它存在于服务器中,我想突出显示我创建的复选框。当我做.match它发现所有包含的变量都说字符foo但我需要完全匹配,而不仅仅是它包含。

见下面我目前的情况。 (请注意,变量不是一成不变的,有些可能会有所不同,这些变量将包含在其他变量中,这就是为什么它们需要精确的原因)

var industriesget = [ "Servers & Software", "Environmental Issues", "Hotels & Tourism"];
var industries = [ "Servers & Software", "Environmental Issues", "Hotels & Tourism", "Liquor, Wine & Beer", "Defense Industries", "Publishing & Printing", "Real Estate", "Not For Profit", "Food Services", "Corporate & Banking", "Agriculture & Fishing", "Communications", "Manufacturing", "Mining", "Entertainment & Gambling", "Retail Sales", "Signage & Fitouts", "Construction", "Transportation", "Public Utilities", "Education" ];

for ( var i = 0; i < industries.length; i++ ) {
//another for loop to run through the matches?
if..........
  document.write( "<label for='" + industries[ i ] + "' class='col-md-2 col-xs-5' style=' float:left; height:55px;' valign='middle'>" + industries[ i ] + "<input valign='middle' type='checkbox' style='vertical-align: middle;text-align: center;' name='products' id='" + industries[ i ] + "' checked></label>" );
  
 else{
 document.write( "<label for='" + industries[ i ] + "' class='col-md-2 col-xs-5' style=' float:left; height:55px;' valign='middle'>" + industries[ i ] + "<input valign='middle' type='checkbox' style='vertical-align: middle;text-
 }
 
 
}

2 个答案:

答案 0 :(得分:0)

这是你在找什么?这是最简单的方法。

&#13;
&#13;
var industriesget = [ "Servers & Software", "Environmental Issues", "Hotels & Tourism"];
var industries = [ "Servers & Software", "Environmental Issues", "Hotels & Tourism", "Liquor, Wine & Beer", "Defense Industries", "Publishing & Printing", "Real Estate", "Not For Profit", "Food Services", "Corporate & Banking", "Agriculture & Fishing", "Communications", "Manufacturing", "Mining", "Entertainment & Gambling", "Retail Sales", "Signage & Fitouts", "Construction", "Transportation", "Public Utilities", "Education" ];




for ( var i = 0; i < industries.length; i++ ) {
//another for loop to run through the matches?
if(industriesget.indexOf(industries[i])!== -1)
  document.write( "<label for='" + industries[ i ] + "' class='col-md-2 col-xs-5' style=' float:left; height:55px;' valign='middle'>" + industries[ i ] + "<input valign='middle' type='checkbox' style='vertical-align: middle;text-align: center;' name='products' id='" + industries[ i ] + "' checked></label>" );
  
 else{
 document.write( "<label for='" + industries[ i ] + "' class='col-md-2 col-xs-5' style=' float:left; height:55px;' valign='middle'>" + industries[ i ] + "<input valign='middle' type='checkbox' style='vertical-align: middle;text-")
 }
 
 
}
&#13;
&#13;
&#13;

如果您想查看industriesget是否属于行业的子集,请尝试使用

var intersectionIndustries = industries.filter(function(n) {
    return industriesget.indexOf(n) !== -1;
});

并检查

(intersectionIndustries.length == industriesget.length)

,如果为true则为其子集,否则行业中存在不存在的条目。

答案 1 :(得分:0)

使用此: 将两个数组传递给此函数并检查该标志是否为真:

        var industriesget = [ "Servers & Software", "Environmental 
         Issues", "Hotels & Tourism"];
        var industries = [ "Servers & Software", "Environmental Issues", "Hotels 
       & Tourism", "Liquor, Wine & Beer", "Defense Industries", "Publishing & 
       Printing", "Real Estate", "Not For Profit", "Food Services", "Corporate & 
       Banking", "Agriculture & Fishing", "Communications", "Manufacturing", 
       "Mining", "Entertainment & Gambling", "Retail Sales", "Signage & 
      Fitouts", 
      "Construction", "Transportation", "Public Utilities", "Education" ];

        if(arr(industriesget,industries)) {
            alert("contains");
        }

我们的职能是:

function arr(industriesget,industries)
 {
   var flag=false;
    for(var i=0;i<industriesget.length;i++)
     {
        if(industries.includes(industriesget[i])) {           
           flag = true; 
        }
     }
return flag;
}