0
我已經使用以前發佈的幫助來獲得目前爲止的效果,但我無法使其工作。我想測試group1數組中是否存在所有group2數組元素。目前,我得到的結果與我想要的相反。我試着在'比較數組'的jQuery部分中反轉代碼,但沒有運氣。示例結果在下面的列中給出。 https://jsfiddle.net/v82pfx2w/。任何人都可以闡明這一點嗎?測試另一個陣列內陣列的元素
group1: group1: group1:
Luxembourg Greece Greece
Netherlands Netherlands Netherlands
Belgium Luxembourg Luxembourg
Belgium Belgium
group2
Netherlands group2 group2
Greece Netherlands Luxembourg
Luxembourg Luxembourg Belgium
Belgium Belgium
NO MATCH YES MATCH YES MATCH
https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
<div id="containerStore">
<div class='AggregCountriesBNLUX'>
<label>Netherlands</label>
<label>Greece</label>
<label>Luxembourg</label>
<label>Belgium</label>
</div>
<div class='AggregCountriesWEur'>
<label>Netherlands</label>
<label>Luxembourg</label>
<label>Belgium</label>
</div>
<div class='AggregCountriesWEur'>
<label>Luxembourg</label>
<label>Belgium</label>
</div>
</div>
var group1 = [];
group1 = ['Luxembourg', 'Netherlands', 'Belgium']
$('#containerStore div[class^="AggregCountries"]').each(function(i, obj) {
group2 = $(this).map(function() {
return $.trim($(this).text());
})
.get();
group2 = group2.join();
// compare arrays:
var compareGroups = group1.every(function(val) {
return group2.indexOf(val) >= 0;
})
alert(compareGroups);
});
謝謝,但我得到一個錯誤'group2.every不是一個函數'。 – Silverburch
@Silverburch用小提琴更新了答案。 – adiga
啊,原因是我沒有'找到'標籤。非常感謝您的幫助! – Silverburch