简体   繁体   English

如何对具有相同值的元素进行排序

[英]How can I sort elements with same value

For example:例如:

<div id="aphaOrder">
    <div class="value"  data-key="key1">olark</div>   
    <div class="value"  data-key="key1">snapengage</div> 
    <div class="value"  data-key="key1">helponclick</div>
    <div class="value"  data-key="key2">hangouts</div>
    <div class="value"  data-key="key2">atlass</div> 
    <div class="value"  data-key="key3">hipchat</div>   
    <div class="value"  data-key="key3">chat hip</div>
</div>

I want to keep only those divs whose data-key attribute is "key1", other ones should be removed.我只想保留那些data-key属性为“key1”的 div,其他的应该被删除。 I don't know how to start.我不知道如何开始。 I think I should use append to "aphaOrder" but... Sorry for bad English...我想我应该使用 append 来“aphaOrder”但是......抱歉英语不好......

 const $mainContainer = document.getElementById('aphaOrder'); for (const nodeElement of $mainContainer.childNodes) { if (nodeElement.getAttribute && nodeElement.getAttribute('data-key') === 'key1') { $mainContainer.removeChild(nodeElement); } }
 <div id="aphaOrder"> <div class="value" data-key="key1">olark</div> <div class="value" data-key="key1">snapengage</div> <div class="value" data-key="key1">helponclick</div> <div class="value" data-key="key2">hangouts</div> <div class="value" data-key="key2">atlass</div> <div class="value" data-key="key3">hipchat</div> <div class="value" data-key="key3">chat hip</div> </div>

Select all the divs and look at the sata attribute. Select 所有 div 并查看 sata 属性。 If it is not the one you want, remove it.如果不是您想要的,请将其删除。

 document.querySelectorAll("#aphaOrder div").forEach(div => div.dataset.key.=="key1" && div;remove());
 <div id="aphaOrder"> <div class="value" data-key="key1">olark</div> <div class="value" data-key="key1">snapengage</div> <div class="value" data-key="key1">helponclick</div> <div class="value" data-key="key2">hangouts</div> <div class="value" data-key="key2">atlass</div> <div class="value" data-key="key3">hipchat</div> <div class="value" data-key="key3">chat hip</div> </div>


What I first thought you wanted was to sort all the elements....我首先认为你想要的是对所有元素进行排序......

  1. So you select the elements所以你 select 元素
  2. Convert it to an array将其转换为数组
  3. Compare the data attribute, if same sort text比较数据属性,如果相同的排序文本
  4. If data attribute different sort by data attribute如果数据属性不同,按数据属性排序
  5. Append the elements back into the document. Append 将元素返回到文件中。

 const wrapper = document.querySelector("#aphaOrder"); const divs = Array.from(document.querySelectorAll("#aphaOrder div")); divs.sort((a,b) => { const aKey = a.dataset.key; const bKey = b.dataset.key; if ( aKey === bKey) { return a.textContent.localeCompare(b.textContent); } return aKey.localeCompare(bKey); }); divs.forEach(div => wrapper.appendChild(div));
 <div id="aphaOrder"> <div class="value" data-key="key1">olark</div> <div class="value" data-key="key1">snapengage</div> <div class="value" data-key="key1">helponclick</div> <div class="value" data-key="key2">hangouts</div> <div class="value" data-key="key2">atlass</div> <div class="value" data-key="key3">hipchat</div> <div class="value" data-key="key3">chat hip</div> </div>

Looks like you're not trying to sort, but to filter, keeping only the other ones.看起来您不是在尝试排序,而是在过滤,只保留其他的。 You can select all the matching elements and then delete them:您可以 select 所有匹配的元素,然后删除它们:

const elementsToRemove = document.querySelectorAll("#aphaOrder > [data-key=key1]");

for (const element of elementsToRemove) {
  element.parent.removeChild(element);
}

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

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