简体   繁体   English

如何从javascript中的数组中获取多个随机不同的项目?

[英]How do I get multiple random different items from an array in javascript?

function question1(){
    if(sessionStorage.quizCounter == 0){
        var qa0 = qs[Math.floor(Math.random() * 19)];
        document.getElementById('questions_container').innerHTML = qa0[0]

        for (let i = 1; i <= 3; i++) {
            document.getElementById('answers_container').innerHTML += qa0[Math.floor((Math.random() * 2))+1];
        }
    }
}

That's my function, it's supposed to place the first item in the array in a <div id="questions_container"> which works, but the second part - the for loop - doesn't work.那是我的函数,它应该将数组中的第一项放在有效的<div id="questions_container">中,但第二部分 - for 循环 - 不起作用。

The for loop is supposed to paste the last 3 items randomly in <div id="answers_container"> . for 循环应该在<div id="answers_container">随机粘贴最后 3 个项目。 And I don't know how to do that beyond the idea.我不知道如何超越这个想法。 As it stands now it prints duplicates, which I don't want.就目前而言,它打印了我不想要的重复项。

qa0[0] is always the question. qa0[0] 始终是个问题。 qa0[1, 2, 3] is always the answers. qa0[1, 2, 3] 总是答案。 qa0[] contains always 4 items. qa0[] 始终包含 4 个项目。 I need the first item to always be on qa0[0].我需要第一个项目始终在 qa0[0] 上。

I figured to answer this question in the easiest possible way.我想以最简单的方式回答这个问题。

If you dont need to worry about the positions, you can just randomize the data and then remove the N items off the end.如果您不需要担心位置,您可以随机化数据,然后从末尾删除 N 项。

function shuffle(array) {
  array.sort(() => Math.random() - 0.5);
}

This will shuffle your array, essentially randomizing it.这将打乱你的数组,基本上是随机化它。 You can then pop the items off the end然后,您可以将项目从末尾弹出

function randomitems(arr, returnedCount){
    function shuffle(array) {
      array.sort(() => Math.random() - 0.5);
    }
    const clonedData = arr.slice()
    shuffle(clonedData)
    if ( returnedCount === undefined) return clonedData;
    return clonedData.slice(-1 * returnedCount)
}

This will take up some space because I dont want to change the original object.这将占用一些空间,因为我不想更改原始对象。

Smooth, and Quick.流畅,快速。 No super complex code.没有超级复杂的代码。

It will return a random list, and it will return only duplicates IF there are duplicates in the base arr.它将返回一个随机列表,如果基 arr 中有重复项,它将仅返回重复项。 Otherwise, it will maintain uniqueness of array items.否则,它将保持数组项的唯一性。

This will return an array of items, you can then loop that list or do whatever you specifically need with the desired results.这将返回一个项目数组,然后您可以循环该列表或使用所需的结果执行任何您特别需要的操作。

If you want to get super Computer Science-y, I can also give a hyper optimized random set, but that wouldn't likely see much in terms of returns until your dataset started to get big.如果你想获得超级计算机科学,我也可以提供一个超优化的随机集,但在你的数据集开始变大之前,这在回报方面不太可能看到。

This would be used in the following case for you:这将在以下情况下为您使用:

// This will link all the questions and possible answers together.
qa0 = [question, ans1, ans2, ans3, ans4].
// The next step is that you need to shuffle the answers.
tmpVar = [].concat(qa0[0], randomizeItems(qa0.slice(1))); // no number will just return the shuffled set.
qa0 = tmpVar;  // You could likely just not even use this tmpVar and just do assignment if you really wanted to.

That way, qa0, will have the question be the 0 index, and all items after are shuffled.这样,qa0 的问题将是 0 索引,之后的所有项目都将被打乱。 So it would then look like: qa0 = [ question, ans3, ans1, ans2, ans4];所以它看起来像: qa0 = [ question, ans3, ans1, ans2, ans4];

let myArray = [1, true, "Hello world!"]; // for example

console.log(myArray[Math.floor(Math.random() * myArray.length)]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM