为了账号安全,请及时绑定邮箱和手机立即绑定

jquery - 检查数组中是否存在某些订单

jquery - 检查数组中是否存在某些订单

炎炎设计 2023-09-21 10:47:25
我有一个数组中的元素列表,元素包含大鼠,猫,狗,狼,老虎,狮子,大象可以通过拖动元素来切换元素的位置以重新排列顺序。单击“获取元素顺序”按钮后,它会显示元素的顺序,例如位置 1 是老鼠。我想在提示订单后提示一个警告框。我想检查排列的元素列表中是否存在某些特定顺序。例如,“tiger < lion <rat”会提示一个通知(例如“Specific Order Found!!”),因为顺序如下:猫、老虎、狼、狮子、狗、大象、老鼠它们不需要是邻居,但如果在列表中老虎在狮子前面,狮子在老鼠前面,按照顺序,就会有消息提示。有没有一种方法可以验证整个列表是否包含指定订单?代码如下:    <head>    <title>The order of Sortable Element</title>     <meta name="viewport" content="width=device-width, initial-scale=1">    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.3/themes/hot-sneaks/jquery-ui.css" />    <script src="http://code.jquery.com/jquery-2.1.3.js"></script>    <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>    <style>        div.sortIt { width: 120px; background-color: #44c756; font-family: Verdana;            float: left; margin: 4px; text-align: center; border: medium solid #999;        padding: 4px; color:#eee; box-shadow:5px 5px 5px #444;}          </style>    <script>        $(document).ready(function() {   $('#sortableContainer').sortable();   $('<br><br><div id=buttonDiv><button>Get Order of Elements</button></div>').appendTo('body');   $('button').button().click(function() {   var itemOrder = $('#sortableContainer').sortable("toArray");   for (var i = 1; i < itemOrder.length; i++) {    alert("Position: " + i + " ID: " + itemOrder[i-1]);   }            })        });    </script></head> <body> <div id="sortableContainer">        <div id="rat" class="sortIt">rat</div>        <div id="cat" class="sortIt">cat</div>        <div id="dog" class="sortIt">dog</div>        <div id="wolf" class="sortIt">wolf</div>        <div id="tiger" class="sortIt">tiger</div>        <div id="lion" class="sortIt">lion</div>        <div id="elephant" class="sortIt">elephant</div>    </div>  <br><br><br>  <p>The position is one based.</p></body></html>
查看完整描述

1 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

在这里,我为您创建了一个帮助函数,它将动物订单作为要检查的字符串,格式为"animal < animal"... 以及要检查的动物数组,您可以选择所需的任何动物数组,两个数组的大小并不重要所有动态,因此它适合很多用途,现在我认为您可以轻松完成其余的工作:)


let animals = ["cat", "tiger", "wolf", "lion", "dog", "elephant", "rat"];


function checkAnimalsOrder(animalsArr, orderStr) {

  // store the current index of the orderedAnimals array

  let ind = 0;

  // make an array from that string format "animal > animal" ...;

  let orderedAnimals = orderStr.split(" < ");

  // filter the animals array to get only the animal that have the same index

  // of the orderedAnimals array element

  return animalsArr.filter(function(animal, index) {

    if(animalsArr[index] === orderedAnimals[ind]) {

      ind++;

      return animal;

    }

  }).join("") === orderedAnimals.join("");

  // finally join the two arrays as a string and check for equality

}


// Testing 

console.log("checking for 'tiger < lion < rat':");

console.log(checkAnimalsOrder(animals, "tiger < lion < rat"));


console.log("checking for 'tiger < dog < rat':");

console.log(checkAnimalsOrder(animals, "tiger < dog < rat"));


console.log("checking for 'tiger < cat < rat':");

console.log(checkAnimalsOrder(animals, "tiger < cat < rat"));


// On the fly

console.log("checking for 'rat < tiger':");

console.log(checkAnimalsOrder(["rat", "elephant", "tiger"], "rat < tiger"));


查看完整回答
反对 回复 2023-09-21
  • 1 回答
  • 0 关注
  • 52 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信