简体   繁体   English

JavaScript或jQuery使用数据属性比较两个有序列表

[英]JavaScript or jQuery compare two ordered lists using data-attribute

I want to be able to compare two lists using data attribute in either Javascript or jQuery. 我希望能够使用Javascript或jQuery中的data属性比较两个列表。 I can't find an example of this anywhere and don't know how to go about it. 我在任何地方都找不到这样的例子,也不知道如何去做。

The first list is static and the second list is sortable. 第一个列表是静态的,第二个列表是可排序的。 Once the user sorts the lists in the right order, [data-compare 1 to 4] in both lists, they click a button to check if they got the matches correct. 用户以正确的顺序对列表进行排序后,即两个列表中的[数据比较1到4],他们都会单击按钮以检查匹配是否正确。

There are examples of setting the correct order 1 to X in a single list, but, none I can find to compare two lists. 在单个列表中有一些将正确的1设置为X的示例,但是我找不到两个列表进行比较。

The basic HTML: 基本的HTML:

<ul>
    <li data-compare="1">Bananas</li>
    <li data-compare="2">tomatoes</li>
    <li data-compare="3">carrots</li>
    <li data-compare="4">dates</li>
  </ul>

  <ul>
    <li data-compare="1">Tree</li>
    <li data-compare="4">Palm tree</li>
    <li data-compare="2">Bush</li>
    <li data-compare="3">In the ground</li>
  </ul>

You can first get two arrays with data-compare values and then use every method to check if each element is the same in both arrays. 您可以首先获取两个具有data-compare值的数组,然后使用every方法检查两个数组中的每个元素是否相同。

 const getData = arr => { return arr.map(function() { return $(this).attr('data-compare') }).get(); } const l1 = getData($('ul:nth-of-type(1) li')) const l2 = getData($('ul:nth-of-type(2) li')) const check = l1.every((e, i) => l2[i] == e); console.log(check) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li data-compare="1">Bananas</li> <li data-compare="2">tomatoes</li> <li data-compare="3">carrots</li> <li data-compare="4">dates</li> </ul> <ul> <li data-compare="1">Tree</li> <li data-compare="4">Palm tree</li> <li data-compare="2">Bush</li> <li data-compare="3">In the ground</li> </ul> 

If an ordered list has data-compare attributes with the values 1, 2, 3, 4, you can just check if the the value of the data-compare is identical to the list item's index + 1: 如果有序列表的data-compare属性的值为1、2、3、4,则只需检查data-compare的值是否等于列表项的索引+ 1:

 function checkOrdered(list) { return Array.from(list).every((item, i) => item.getAttribute('data-compare') == (i + 1)); } console.log(checkOrdered(document.querySelectorAll('#list1 > li'))); console.log(checkOrdered(document.querySelectorAll('#list2 > li'))); 
 <ul id="list1"> <li data-compare="1">Tree</li> <li data-compare="4">Palm tree</li> <li data-compare="2">Bush</li> <li data-compare="3">In the ground</li> </ul> <ul id="list2"> <li data-compare="1">Tree</li> <li data-compare="2">Bush</li> <li data-compare="3">In the ground</li> <li data-compare="4">Palm tree</li> </ul> 

You can do the same thing using another list as the compare basis: 您可以使用另一个列表作为比较基础来执行相同的操作:

 function checkOrdered(list1, list2) { return Array.from(list1).every((item, i) => item.getAttribute('data-compare') === list2[i].getAttribute('data-compare')); } const src = document.querySelectorAll('#src > li'); const target1 = document.querySelectorAll('#target1 > li'); const target2 = document.querySelectorAll('#target2 > li'); console.log(checkOrdered(src, target1)); console.log(checkOrdered(src, target2)); 
 <ul id="src"> <li data-compare="1">Bananas</li> <li data-compare="2">tomatoes</li> <li data-compare="3">carrots</li> <li data-compare="4">dates</li> </ul> <ul id="target1"> <li data-compare="1">Tree</li> <li data-compare="4">Palm tree</li> <li data-compare="2">Bush</li> <li data-compare="3">In the ground</li> </ul> <ul id="target2"> <li data-compare="1">Tree</li> <li data-compare="2">Bush</li> <li data-compare="3">In the ground</li> <li data-compare="4">Palm tree</li> </ul> 

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

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