简体   繁体   English

(JS)如何从数组中随机提取 3 个唯一元素,除了第 0 个索引?

[英](JS) How can I randomly pull 3 unique elements from an array, EXCEPT for the 0th index?

I'm building a quiz app for a school project, and I want to pull 3 unique, random elements from an array of possible answers.我正在为一个学校项目构建一个测验应用程序,我想从一系列可能的答案中提取 3 个独特的随机元素。 However, I want the 0th index to be excluded, as this is the correct answer and will always be added separately.但是,我希望排除第 0 个索引,因为这是正确的答案,并且总是会单独添加。

const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5", "Answer 7"]

I'm extremely new to JS and have spent some time looking into a way to handle this.我对 JS 非常陌生,并且花了一些时间寻找一种方法来处理这个问题。 Apologies in advance if it's an accessible solution, I might not be wording my question well.如果这是一个可访问的解决方案,请提前道歉,我可能无法很好地表达我的问题。

The following should do it for you.以下应该为您完成。 slice(1) will return an array without the first element. slice(1)将返回一个没有第一个元素的数组。 The sort method will generally randomize the sorting of the array. sort方法一般会随机化数组的排序。 Finally, slice(0, 3) will grab the first three elements of the randomly sorted array.最后, slice(0, 3)将抓取随机排序数组的前三个元素。

 const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5", "Answer 7"]; const result = answers.slice(1).sort(() => 0.5 - Math.random()).slice(0, 3); console.log(result);

 function getRandomUniqueFromArrayExceptHead(array, amount) { const [_head, ...arrayCopy] = array return (new Array(amount)).fill(0).map(() => { const index = Math.floor(Math.random() * arrayCopy.length) return arrayCopy.splice(index, 1)[0] }) } const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5", "Answer 7"] console.log(getRandomUniqueFromArrayExceptHead(answers, 3))

You may try this:你可以试试这个:

 const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5", "Answer 7"]; const result = answers.slice(1).sort(() => Math.random() - Math.random()).slice(0, 3); console.log(result);

 const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer" + " 4", "Answer 5", "Answer 7"]; function random(min,max){ var answer = Math.round((Math.random() * max) + min); return answer; } var j = answers.length - 2; var object = []; var locations = []; function find(array,value){ var location = "not present"; for(var i = 0;i < array.length;i++){ if(array[i] == value){ location = i; break; } } return location; } var bucket; for(var i = 0;i < 3;i++){ bucket = random(1,j); object[i] = answers[bucket]; while(find(locations,bucket),= "not present"){ object[i] = answers[random(1;j)]; } location[i] = bucket. } document.getElementById("demo");innerHTML = object;
 <html> <p id="demo"></p> </html>

First, try building a random method by using Math.random() by, first multiplying the result by the maximum number next, adding the minimum number then, round the result.首先,尝试使用 Math.random() 构建一个随机方法,首先将结果乘以最大数,然后加上最小数,然后对结果进行四舍五入。 Next, store two minus the length of answers in a variable.接下来,将减去答案长度的 2 存储在变量中。 Then, get a random number from 1 to the variable you used earlier.然后,从 1 到您之前使用的变量获取一个随机数。 Finally, store the previous result in another variable (Repeat process if location is found in the location array).最后,将先前的结果存储在另一个变量中(如果在位置数组中找到位置,则重复过程)。 (Or do whatever you want with it). (或者用它做任何你想做的事情)。 Store the location in another variable Repeat the following process 3 times.将位置存储在另一个变量中 重复以下过程 3 次。

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

相关问题 如何访问此嵌套对象中数组“最喜欢的食物”的第 0 个索引? - How to access 0th index of array 'favorite foods' in this nested objects? 从数组3中乘3随机提取元素 - Pull elements randomly from an array 3 by 3 如何更新反应 state 中存在的字符串数组的第 0 个索引,保持所有其他索引值不受影响 - How to update 0th index of array of strings present in react state keeping all other index values unaffected 无法在 JavaScript 中的第 0 个 position 处设置数组索引 - Unable to set array index at 0th position in JavaScript 如何从表行中删除除第一行以外的所有元素 - how to remove all elements from a table row except first th 如何使用 hash 将元素从一个数组伪随机移动到另一个数组? - How can I move elements from one array to another pseudo-randomly using a hash? 数组第0个成员上第一个孩子的用法 - Usage of first child on 0th member of array 如何使用Backbone.js从索引中提取新请求? - How can I pull a new request from my index with Backbone.js? 如何拼接每个第0个索引特定键“逻辑”和嵌套JSON的值? - How to splice every 0th index particular key “logic” & value of a nested JSON? 如何在第0个索引特定键“逻辑”和嵌套JSON的值下拼接每个“条件”? - How to splice every “conditions” under 0th index particular key “logic” & value of a nested JSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM