简体   繁体   English

jQuery:如何遍历一组仅查找与另一个数组中的值匹配的元素?

[英]jQuery: How can I loop over a set of elements finding only those matching the values within another array?

I've basically got a little function called findItem() that is supposed to be finding the elements I'm looking for based on custom data- attributes on the element. 我基本上有一个名为findItem()的小函数,该函数应该根据元素上的自定义data-属性来查找我要查找的元素。

In this case these are purely numerical eg. 在这种情况下,这些都是纯数字形式的。 data-slide=1 . data-slide=1

I'm a bit clueless as to how to match the value of each item's data-slide to one that is contained within the other array. 我对如何将每个项目的数据幻灯片的值与另一个数组中包含的数据幻灯片的值一无所知。

Here is a more concrete example: 这是一个更具体的示例:

function findItem(count) {
    var collection = [];

    $.each(allMyLiItems, function(i, item) {

        if ( $(item).data('slide') == count ) { 
            collection.push(item);
        }

    });

    return $(collection);
}
findItem([1,3])

which does not work because count inside the if statement does not seem to match anything. 这不起作用,因为在if语句中count似乎与任何内容都不匹配。

The page does contain 4 <li data-slide="{number}">… elements so 1,3 should return the first and third of those elements. 该页面确实包含4个<li data-slide="{number}">…元素,因此1,3应该返回这些元素的第一个和第三个。

What am I doing wrong here? 我在这里做错了什么?

Use jQuery.grep and jQuery.inArray : 使用jQuery.grepjQuery.inArray

function findItem(items) {
    return jQuery.grep($('li'), function(element, index) { 
       return jQuery.inArray($(element).data('slide'), items) != -1;
    });
}

Working example 工作实例

To fix the issue with the count inside the if statment not matching anything, try this: 要解决if语句中的count不匹配的问题,请尝试以下操作:

function findItem(count) {
    var collection = [],
             count = count;

    $.each(allMyLiItems, function(i, item) {

        if ( $(item).data('slide') == count ) { 
            collection.push(item);
        }

    });

    return $(collection);
}
findItem([1,3])

This will create a closure so the anonymous function inside the each will be able to see it. 这将创建一个闭包,因此each闭包中的匿名函数将能够看到它。

Your second issue is the fact that you're passing count as an array. 第二个问题是您将count作为数组传递。 So the if condition needs a little fixing: 因此, if条件需要一些修复:

function findItem(count) {
    var collection = [],
             count = count;

    $.each(allMyLiItems, function(i, item) {

        if ( count.indexOf($(item).data('slide')) !== -1 ) { 
            collection.push(item);
        }

    });

    return $(collection);
}
findItem([1,3])

暂无
暂无

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

相关问题 如何遍历一组ID并匹配jQuery中另一组元素的活动元素属性? - How can I loop through a set of IDs and match the active element's attribute of another set of elements in jQuery? 如何在 jquery 上循环一个值数组? - how can I loop on jquery An array of values? 如何比较来自不同元素的2个值,并且仅对具有匹配值的元素采取措施? - How can I compare 2 values from different elements and take action ONLY for elements that have matching values? 在一个数组中查找比另一个数组大的元素 - Finding Elements in an Array that are larger than those in another Array 如何遍历数组以获取 jquery 属性? - How can i loop over an array to get jquery properties? 在jQuery中,如何在每个循环中动态删除数组元素? - In jQuery, how can I in an each loop remove array elements dynamically? jquery:将一组元素粘贴到另一组元素/合并元素上 - jquery: Paste a set of elements over another set of elements / merging elements jQuery:not:contains 隐藏与数组值不匹配的元素 - jQuery :not :contains to hide elements not matching array values 如何使用 Javascript 循环遍历数组并更改和添加额外的 HTML,以输出带有这些值的一些文本? - How Can I use Javascript to loop through an array and change and add extra HTML that outputs some text with those values? jQuery仅循环其中具有值的那些输入 - Jquery loop through only those inputs which has values in it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM