简体   繁体   English

jQuery-仅选择与性能匹配的第一个元素

[英]jQuery - Select only the first element that matches for performance

Is it possible to write a jQuery selector that only selects the first element that matches the criteria and stops there? 是否可以编写仅选择符合条件的第一个元素并在那里停止的jQuery选择器?

I am aware of the not-so-performant :first selector and the better .first() call, but both appear to select all the elements and then only return the one at position [0] . 我知道性能不高的:first选择器和更好的.first()调用,但是它们似乎都选择了所有元素,然后只在位置[0]返回一个。

Let's say I have 100 rows in a table and I need to do something to all of them. 假设我在一个表中有100行,并且我需要对所有这些做一些事情。 They don't have IDs, but rather a data-guid attribute with a unique value. 它们没有ID,但是具有唯一值的data-guid属性。

Here's an example with a list: 这是带有列表的示例:

<ol>
    <li data-guid="guid_1">A</li>
    <li data-guid="guid_2">B</li>
    <li data-guid="guid_3">C</li>
    <li data-guid="guid_4">D</li>
</ol>

.

var data = [
    {guid: "guid_1", new_value: "I"},
    {guid: "guid_2", new_value: "II"},
    {guid: "guid_3", new_value: "III"},
    {guid: "guid_4", new_value: "IV"}
];

var $ol = $('#list');
for (var i = 0; i < data.length; ++i) {
    $('li[data-guid="' + data[i].guid + '"]', $ol).text(data[i].new_value);
}

Unfortunately, this would run through each element 100 times, making the total DOM iterations reach 10,000. 不幸的是,这将遍历每个元素100次,使DOM迭代总数达到10,000。 In reality, since GUIDs are unique, it should run through the first element 100 times and the last one only once, making it run 5,050 times. 实际上,由于GUID是唯一的,因此它应在第一个元素中运行100次,而在最后一个元素中仅运行一次,因此它应运行5,050次。

I guess I'm wondering if there's a way to treat it like an ID selector, where it stops when it finds an element that meets the criteria and then breaks out of the loop. 我想我想知道是否有一种方法可以像对待ID选择器一样对待它,在找到符合条件的元素时它会停止,然后跳出循环。

-- -

squint's Solution (if my interpretation is correct) 斜视的解决方案 (如果我的解释正确)

var data = [
    {guid: "guid_1", new_value: "I"},
    {guid: "guid_2", new_value: "II"},
    {guid: "guid_3", new_value: "III"},
    {guid: "guid_4", new_value: "IV"}
];

// Hashmap in case array is not the same order as list items, look up by GUID
var hashmap = {};
for (var i = 0, l = data.length; i < l; ++i) {
    hashmap[data[i].guid] = data[i];
}

var $ol = $('#list');
$('li[data-guid]', $ol).text(function () {
    var item = hashmap[$(this).attr('attr-guid')];
    return item ? item.new_value : 'N/A';
});

this run only data.length times (see console log) 这只运行data.length次(请参阅控制台日志)

    <ol id="list">
     <li data-guid="1">Coffee</li>
     <li data-guid="2">Tea</li>
     <li data-guid="5">Milk</li>
     <li data-guid="6">Water</li>
    </ol> 

    var $ol = $('#list');
    var data = [[1,"Saab"], [2,"Volvo"], [3,"BMW"], [6,"Nissan"]]; 
    for (var i = 0; i < data.length; ++i) {
       $ol.find('li[data-guid="' + data[i][0] + '"]').text(data[i][1]);
       console.log(i);
    }

http://jsfiddle.net/rLbd7fuc/ http://jsfiddle.net/rLbd7fuc/

In jQuery you can use the :first selector to get only fhe first element of the collection, but, quoting the official jQuery documentation: 在jQuery中,您可以使用:first选择器仅获取集合的第一个元素,但是引用官方jQuery文档:

Because :first is a jQuery extension and not part of the CSS specification, queries using :first cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. 因为:first是jQuery扩展,而不是CSS规范的一部分,所以使用:first的查询无法利用本机DOM querySelectorAll()方法提供的性能提升。 To achieve the best performance when using :first to select elements, first select the elements using a pure CSS selector, then use .filter(":first"). 为了在使用:first选择元素时达到最佳性能,请首先使用纯CSS选择器选择元素,然后使用.filter(“:first”)。

Both the :first selector and .filter() method don't really improve/boost your code so much. :first选择器和.filter()方法都没有真正改善/提高您的代码。

If you only want to select the first element, then the best solution is to use document.querySelector() , that stops on the first element and returns it. 如果只想选择第一个元素,那么最好的解决方案是使用document.querySelector() ,它在第一个元素上停止并返回它。

So here it is: 所以这里是:

var $ol = $('#list');
for (var i = 0; i < data.length; ++i) {
    $(document.querySelector('li[data-guid="' + data.guid + '"]'), $ol).text(data.new_value);
}

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

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