第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jquery - 檢查數(shù)組中是否存在某些訂單

jquery - 檢查數(shù)組中是否存在某些訂單

炎炎設計 2023-09-21 10:47:25
我有一個數(shù)組中的元素列表,元素包含大鼠,貓,狗,狼,老虎,獅子,大象可以通過拖動元素來切換元素的位置以重新排列順序。單擊“獲取元素順序”按鈕后,它會顯示元素的順序,例如位置 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條經(jīng)驗 獲得超3個贊

在這里,我為您創(chuàng)建了一個幫助函數(shù),它將動物訂單作為要檢查的字符串,格式為"animal < animal"... 以及要檢查的動物數(shù)組,您可以選擇所需的任何動物數(shù)組,兩個數(shù)組的大小并不重要所有動態(tài),因此它適合很多用途,現(xiàn)在我認為您可以輕松完成其余的工作:)


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 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號