[英]jQuery compare two arrays?
I have an array and I would like to create another new array and the values are extracted from the HTML itself, then I would like to compare both the values and output the match value. 我有一个数组,我想创建另一个新数组,然后从HTML本身中提取值,然后比较两个值并输出匹配值。
My existing array to compare: 我现有的数组进行比较:
var designersArrSelected = ["Von Blan","Foo Can","Diane con Fursternberg","BCBG MAX Azria"];
My HTML 我的HTML
<ul id="seasonsDropDown">
<li data-season="FALL 2013 HAUTE COUTURE">FALL 2013 HAUTE COUTURE
<ul class="designers">
<li data-designer="Alexander Wang">Alexander Wang</li>
<li data-designer="BCBG MAX Azria">BCBG MAX Azria</li>
<li data-designer="Diane con Fursternberg">Diane con Fursternberg</li>
<li data-designer="Diane Von De">Diane Von De</li>
</ul>
</li>
</ul>
To grab the all designer's names from the HTML I've used: 要从我使用的HTML中获取所有设计师的姓名:
var designersArrAll = [];
var i = 0;
$(".designers>li").each(function(){
designersArrAll[i++] = $(this).attr("data-designer");
});
Base on my example the outcome should select "Diane con Fursternberg" and "BCBG MAX Azria since there are two matches, but how should I compare both the arrays for the same values? 根据我的示例,由于存在两个匹配项,因此结果应选择“ Diane con Fursternberg”和“ BCBG MAX Azria”,但是我应该如何比较两个数组的相同值?
Here is the pen: http://codepen.io/vincentccw/pen/eDrox 这是笔: http : //codepen.io/vincentccw/pen/eDrox
Try with $.inArray
. 尝试使用
$.inArray
。 You may use it like here: 您可以在这里使用它:
var found = $.inArray('pattern', stringToBeSearched) > -1;
The result with be a number indicating where in your string begins a pattern you are looking for. 结果为数字,指示您的字符串在您要查找的模式中的何处开始。
-1
will let you know that there is no such a pattern in the stringToBeSearched. -1
将使您知道stringToBeSearched中没有这样的模式。
If you want to do it the old-fashioned for loop way, here's some code i picked up here: How do I check if an array includes an object in JavaScript? 如果您想使用老式的for循环方式,请在此处获取以下代码: 如何检查数组是否包含JavaScript中的对象? .
。 An if conditional inside each, you could style elements while matches are hit.
如果每个条件内部都有条件,则可以在命中匹配时为元素设置样式。 Probably not the most efficient, but it works.
可能不是最有效的,但它可以工作。
fiddle: http://jsfiddle.net/LmQ9U/ 小提琴: http : //jsfiddle.net/LmQ9U/
var designersArrAll = [];
var designersArrSelected = ["Von Blan","Foo Can","Diane con Fursternberg","BCBG MAX Azria"];
var i = 0;
$(".designers>li").each(function(){
designersArrAll[i++] = $(this).attr("data-designer");
});
$(".designers>li").each(function(){
designer = $(this).attr("data-designer");
designersArrAll[i++] = designer;
if ( contains( designersArrSelected , designer) ) {
// do something here
$(this).css('background', 'yellow');
}
});
function contains(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
You can do this instead of the loop you have to make the designersArrAll
array. 您可以执行此操作,而不必执行必须创建
designersArrAll
数组的循环。
var designersArrAll = $.map( $('.designers > li'), function(el){
return $(el).data('designer');
});
As for comparing two arrays you can use the code on this question: How to know if two arrays have the same values 至于比较两个数组,您可以在以下问题上使用代码: 如何知道两个数组是否具有相同的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.