如何从Javascript中的旧数组中获取包含一些随机元素的新数组 [英] How to get a new array containing some random elements from an old array in Javascript
本文介绍了如何从Javascript中的旧数组中获取包含一些随机元素的新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用 React Native 制作一个测验应用程序,它有这个对象数组,其中每个对象都包含一个问题陈述、选择和正确的选择.这里总共有 15 个对象,但我需要一个新数组,其中只包含这个数组的 5 个对象.每次我开始测验时都应该随机选择它们.如何获取该数组,帮助
I am making a quiz app in react native, it has this array of objects in which each object contains a question statement,choices and the correct choice. There will be total 15 objects in this, but i require a new array that contains only 5 of the objects of this array. They should be selected at random everytime i start the quiz. How to get that array, help
const questions = [
new quizQuestions(
'What is capital of Pakistan?',
['Karachi', 'Islamabad', 'Lahore', 'Peshawar'],
'Islamabad',
),
new quizQuestions(
'What is capital of India?',
['delhi', 'Islamabad', 'zcjzj', 'Peshawar'],
'delhi',
),
new quizQuestions(
'What is capital of Pakistan?',
['Karachi', 'Islamabad', 'Lahore', 'Peshawar'],
'Islamabad',
),
]
推荐答案
您需要的非常随意.
使用生成器函数
:
const questions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
function* randomise(arr, numOfElem) {
// Create a copy of question so that wont effect actual array
let copyArray = [...arr];
// Use copyArray to generate random as closure, so that it never go out of index
const getRandom = () => Math.floor(Math.random() * copyArray.length);
// This is generator function run always
while (true) {
// If numOfElem is greater, then clone again
// suppose you have 15 question but you take 6. SO after 2 iteration, copyArray will have only 3 element so clone again
if (numOfElem > copyArray.length) {
// Clone again
copyArray = [...arr];
}
// pick 5 random data.
let result = [];
for (let i = 0; i < numOfElem; i++) {
result.push(copyArray.splice(getRandom(), 1)[0]);
}
yield result;
}
}
const questionGenerator = randomise(questions, 5);
console.log(questionGenerator.next().value); // random [ 11, 3, 7, 2, 8 ]
console.log(questionGenerator.next().value); // random [ 12, 13, 14, 9, 5 ]
console.log(questionGenerator.next().value); // random [ 6, 1, 4, 15, 10 ]
console.log(questionGenerator.next().value); // random [ 14, 12, 8, 2, 13 ]
console.log(questionGenerator.next().value); // random [ 9, 3, 10, 6, 11 ]
相同但没有生成器函数
.
const questions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
const randomise = (arr, numOfElem) => {
let copyArray = [...arr];
const getRandom = () => Math.floor(Math.random() * copyArray.length);
return () => {
// If numOfElem is greater, then clone again
// suppose you have 15 question but you take 6. SO after 2 iteration, copyArray will have only 3 element so clone again
if (numOfElem > copyArray.length) {
// Clone again
copyArray = [...arr];
}
// pick 5 random data.
let result = [];
for (let i = 0; i < numOfElem; i++) {
result.push(copyArray.splice(getRandom(), 1)[0]);
}
return result;
};
};
const questionGenerator = randomise(questions, 5);
console.log(questionGenerator()); // random [ 11, 3, 7, 2, 8 ]
console.log(questionGenerator()); // random [ 12, 13, 14, 9, 5 ]
console.log(questionGenerator()); // random [ 6, 1, 4, 15, 10 ]
console.log(questionGenerator()); // random [ 14, 12, 8, 2, 13 ]
console.log(questionGenerator()); // random [ 9, 3, 10, 6, 11 ]
这篇关于如何从Javascript中的旧数组中获取包含一些随机元素的新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文