首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JS随机排序div元素的列表

用JS随机排序div元素的列表
EN

Stack Overflow用户
提问于 2019-11-23 11:44:44
回答 3查看 1K关注 0票数 1

我有一个类别,包含200多个新的项目,在主页上我显示了最后20个新的项目。

代码语言:javascript
复制
<div class="container-category">
   <div class="col-3">item 1</div>
   <div class="col-3">item 2</div>
   <div class="col-3">item 3</div>
   <div class="col-3">item 4</div>
   <div class="col-3">item ..n</div>
</div>

如何使<div class="col-3">列表随机出现在Javascript中?

OrderBy = random

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-11-23 12:01:37

对类别进行洗牌的理想方法是通过后端。但如果你不能,我们可以借shuffle functionality here

使用jQuery get()获取类别div的数组。使用该函数对数组进行洗牌,并使用html()更新内容。

代码语言:javascript
复制
function shuffle(array) {
  var currentIndex = array.length,
    temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

var categories = shuffle($(".container-category>div").get());
$(".container-category").html(categories);
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-category">
  <div class="col-3">item 1</div>
  <div class="col-3">item 2</div>
  <div class="col-3">item 3</div>
  <div class="col-3">item 4</div>
</div>

票数 3
EN

Stack Overflow用户

发布于 2019-11-23 13:42:47

在这里,我展示了埃迪答案的一个变体,它不需要jQuery。The shuffle function

代码语言:javascript
复制
function shuffle(array) {
    var currentIndex = array.length,
        temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}

我们可以利用这一点为div的孩子洗牌如下:

代码语言:javascript
复制
// Get the DOM object of the parent div
const container = document.querySelector('.container-category');

// Turn container.children, which is an HTMLCollection, into an array
// See https://stackoverflow.com/a/222847 for more on this
const children = [...container.children];

// The shuffle function above mutates the argument, so here we shuffle
// the array of children; note that this is not yet reflected in the DOM
shuffle(children);

// Reinsert the children to the parent according to the new order
for (const child of children) {
    container.appendChild(child);
}
票数 1
EN

Stack Overflow用户

发布于 2019-11-23 12:10:25

我的第一个想法是对数组项进行洗牌,但是您有答案,我猜它在大数组中运行得更慢。所以我做了另一个解决方案:

代码语言:javascript
复制
    function getRandIndex(arrayLen) {
      return Math.floor(Math.random() * arrayLen);
    }
    
    function getRandUniqueIndexes(arrayLen, count) {
      const indexes = [];

      while (indexes.length < count) {
        const randIndex = getRandIndex(arrayLen);

        if (!indexes.includes(randIndex)) {
          indexes.push(randIndex);
        }
 
      }

      return indexes;
    }

    // your array with 200 categories
    const categories = Array(200).fill(1).map((_, ndx) => `category-${ndx}`);
    // array with 20 random unique indexes
    const rand20Indexes = getRandUniqueIndexes(categories.length, 20);

    console.log(rand20Indexes.map(catIndex => categories[catIndex]));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59007237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档