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

我有一个数组中的元素列表,元素包含大鼠,猫,狗,狼,老虎,狮子,大象


可以通过拖动元素来切换元素的位置以重新排列顺序。


单击“获取元素顺序”按钮后,它会显示元素的顺序,例如位置 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>


炎炎设计
浏览 55回答 1
1回答

一只名叫tom的猫

在这里,我为您创建了一个帮助函数,它将动物订单作为要检查的字符串,格式为"animal < animal"... 以及要检查的动物数组,您可以选择所需的任何动物数组,两个数组的大小并不重要所有动态,因此它适合很多用途,现在我认为您可以轻松完成其余的工作:)let animals = ["cat", "tiger", "wolf", "lion", "dog", "elephant", "rat"];function checkAnimalsOrder(animalsArr, orderStr) {&nbsp; // store the current index of the orderedAnimals array&nbsp; let ind = 0;&nbsp; // make an array from that string format "animal > animal" ...;&nbsp; let orderedAnimals = orderStr.split(" < ");&nbsp; // filter the animals array to get only the animal that have the same index&nbsp; // of the orderedAnimals array element&nbsp; return animalsArr.filter(function(animal, index) {&nbsp; &nbsp; if(animalsArr[index] === orderedAnimals[ind]) {&nbsp; &nbsp; &nbsp; ind++;&nbsp; &nbsp; &nbsp; return animal;&nbsp; &nbsp; }&nbsp; }).join("") === orderedAnimals.join("");&nbsp; // finally join the two arrays as a string and check for equality}// Testing&nbsp;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 flyconsole.log("checking for 'rat < tiger':");console.log(checkAnimalsOrder(["rat", "elephant", "tiger"], "rat < tiger"));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript