繁体   English   中英

检查数组是否包含在另一个数组中

[英]Check if a array is contained in another

我在下面的一段代码中苦苦挣扎,无法缝在堆栈上的任何地方找到类似的东西。

我尝试使用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- } } 

这是你想要的? 这是最简单的方法。

 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-") } } 

如果要查找行业获取是否是行业的子集,请尝试使用

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

并检查

(intersectionIndustries.length == industriesget.length)

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

使用此方法:将两个数组传递给此函数,然后检查标志是否为true:

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

暂无
暂无

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

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