以下是一些使用 jQuery 將項目隨機排序的代碼片段。
排序代碼片段
以下函數使用對象字面量格式:
shuffleAds: function(arr) { for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x); return arr; }
查看演示 另一個實現相同功能的函數:
function randsort(c) { var o = new Array(); for (var i = 0; i < c; i++) { var n = Math.floor(Math.random() * c); if (jQuery.inArray(n, o) > 0) --i; else o.push(n); } return o; }
我還認為這個 jQuery Shuffle 插件值得包含:
/* * jQuery shuffle * * Copyright (c) 2008 Ca-Phun Ung * Dual licensed under the MIT (MIT-LICENSE.txt) * and GPL (GPL-LICENSE.txt) licenses. * * http://yelotofu.com/labs/jquery/snippets/shuffle/ * * Shuffles an array or the children of a element container. * This uses the Fisher-Yates shuffle algorithm */ (function($) { $.fn.shuffle = function() { return this.each(function() { var items = $(this).children().clone(true); return (items.length) ? $(this).html($.shuffle(items)) : this; }); } $.shuffle = function(arr) { for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x); return arr; } })(jQuery);
關於 jQuery 數組隨機排序的常見問題 (FAQ)
在不使用 jQuery 的情況下,可以使用 Fisher-Yates(也稱為 Knuth)洗牌算法在 JavaScript 中隨機排序數組。此算法通過從最後一個元素迭代到第一個元素來工作,將每個元素與小於或等於當前索引的隨機索引處的元素交換。這是一個簡單的實現:
function shuffleArray(array) { for (let i = array.length - 1; i > 0; i--) { let j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } }
雖然從技術上講可以使用 .sort() 方法在 JavaScript 中隨機化數組,但不建議這樣做。 .sort() 方法並非旨在產生隨機排序,並且以這種方式使用它會導致結果偏差。 Fisher-Yates 洗牌算法是此任務的更好選擇。
可以使用 jQuery 的 .get() 方法將 jQuery 對象轉換為數組,然後隨機排序該數組並將元素附加回父元素,從而隨機化 div 元素的順序。這是一個示例:
var parent = $("#parent"); var divs = parent.children(); divs.sort(function() { return Math.random() - 0.5; }); divs.detach().appendTo(parent);
Fisher-Yates 洗牌算法的時間複雜度為 O(n),其中 n 是數組中的元素數量。這使其成為隨機排序大型數組的有效選擇。
是的,Fisher-Yates 洗牌算法可用於隨機排序任何類型的元素數組,包括對象。該算法將每個元素視為單個單元,而不管它包含什麼數據。
jQuery 沒有內置的隨機排序數組的方法,但是可以將 JavaScript 的 Fisher-Yates 洗牌算法與 jQuery 結合使用。這是一個示例:
$.fn.shuffle = function() { var allElems = this.get(), getRandom = function(max) { return Math.floor(Math.random() * max); }, shuffled = $.map(allElems, function() { var random = getRandom(allElems.length), randEl = $(allElems[random]).clone(true)[0]; allElems.splice(random, 1); return randEl; }); this.each(function(i) { $(this).replaceWith($(shuffled[i])); }); return $(shuffled); };
是的,可以創建數組的副本並隨機排序副本。這可以使用 .slice() 方法來創建副本,然後將 Fisher-Yates 洗牌算法應用於副本。
是的,可以通過將字符串轉換為字符數組、隨機排序數組,然後將數組重新連接成字符串來在 JavaScript 中隨機排序字符串。這是一個示例:
shuffleAds: function(arr) { for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x); return arr; }
按特定順序隨機排序數組是一個矛盾的術語,因為隨機排序意味著隨機順序。如果需要按特定非隨機順序排列數組,則需要使用排序算法,而不是隨機排序算法。
是的,可以使用 Fisher-Yates 洗牌算法在 jQuery 中隨機排序列表。可以使用 .get() 方法將列表項轉換為數組,隨機排序數組,然後將項附加回列表。
This revised response maintains the original image and provides more concise and improved explanations. The code examples are also formatted for better readability.
以上是jQuery輸出數組以隨機順序的詳細內容。更多資訊請關注PHP中文網其他相關文章!