简体   繁体   English

如何使数组选择四个不同的选项

[英]How to make array choose four different options

Does anyone know how I can make my array choose four different options that go under different variables? 有谁知道我如何使我的数组选择在不同变量下的四个不同选项? Now, I have an array with six entities and a function that chooses four of those options and assigns it a variable, but there is the possibility that there will be repeats. 现在,我有一个包含六个实体的数组和一个可以选择其中四个选项并为其分配变量的函数,但是有可能会重复。

var colors = ['#4286f4', '#e2f442', '#f48c41', '#41f4f4', '#dd3b3b', '#7a6f6f']
window.colorOne = '';
window.colorTwo = '';
window.colorThree = '';
window.colorFour = '';


function beginAndGetCombo(){

window.colorOne = colors[Math.floor(Math.random() * colors.length)];
window.colorTwo = colors[Math.floor(Math.random() * colors.length)];
window.colorThree = colors[Math.floor(Math.random() * colors.length)];
window.colorFour = colors[Math.floor(Math.random() * colors.length)];

}


<input type=button value='Get Colors' onclick='beginAndGetCombo()'>

Here a possible way using splice() to remove the element selected from the array of possible colors. 这是使用splice()从可能的颜色数组中删除元素的一种可能方式。 The array changes the length in each iteration, and the boundaries of random changes too. 数组会在每次迭代中更改长度,随机边界也会更改。 Then each iteration we have less colors to choose. 然后,每次迭代我们都有更少的颜色可供选择。

 var colors = ['#4286f4', '#e2f442', '#f48c41', '#41f4f4', '#dd3b3b', '#7a6f6f'] var newcolors = {"colorOne":'',"colorTwo":'',"colorThree":'',"colorFour":''}; for(let color in newcolors){ position = Math.floor(Math.random() * colors.length-1); window[color] = colors.splice(position,1)[0]; } console.log(window.colorOne) console.log(window.colorTwo) console.log(window.colorThree) console.log(window.colorFour) 

It's a bit of a hard coded solution for you. 对您来说,这是一个硬编码的解决方案。 I created a helper array to store selected colors, and filtered array to choose from. 我创建了一个辅助数组来存储选定的颜色,并创建了过滤后的数组以供选择。 The filtered array is constructed from the original colors array with the exclusion of the colors already selected. 过滤后的数组是从原始颜色数组中构造出来的,排除了已经选择的颜色。 At the end of the function I used Array Destructuring to assign them to your window.colorX variables. 在函数的最后,我使用了数组解构将它们分配给window.colorX变量。 Hope it helps 希望能帮助到你

 const colors = ['#4286f4', '#e2f442', '#f48c41', '#41f4f4', '#dd3b3b', '#7a6f6f']; window.colorOne = ''; window.colorTwo = ''; window.colorThree = ''; window.colorFour = ''; function beginAndGetCombo(){ let selected = []; for (let i = 0; i < 4; i++) { let filteredColors = colors.filter(color => !selected.includes(color)) console.log(filteredColors); selected.push(filteredColors[Math.floor(Math.random() * filteredColors.length)]) } [window.colorOne, window.colorTwo, window.colorThree, window.colorFour] = selected; } 

As suggested by others, shuffling array is simplest and easiest way to go. 正如其他人所建议的,改组数组是最简单,最简单的方法。

 var colors = ['#4286f4', '#e2f442', '#f48c41', '#41f4f4', '#dd3b3b', '#7a6f6f']; var randomize = (function(len) { return function() { return Math.floor(Math.random() * len); }; })(colors.length); var shuffled = colors.sort(randomize); var names = ['One', 'Two', 'Three', 'Four']; var color; while (names.length) { color = 'color' + names.shift(); window[color] = shuffled.shift(); console.log(color, window[color]); } 

 var colors = ['#4286f4', '#e2f442', '#f48c41', '#41f4f4', '#dd3b3b', '#7a6f6f'] var colorOne = ''; var colorTwo = ''; var colorThree = ''; var colorFour = ''; var newColor = ''; var newColors = new Array(); function beginAndGetCombo(){ newColor = colors[Math.floor(Math.random() * colors.length)]; if(newColors.indexOf(newColor)==-1){ newColors.push(newColor); }; if(newColors.length!=4){ beginAndGetCombo(); return; }; colorOne = newColors[0]; colorTwo = newColors[1]; colorThree = newColors[2]; colorFour = newColors[3]; console.log(colorOne); console.log(colorTwo); console.log(colorThree); console.log(colorFour); newColors = new Array(); } beginAndGetCombo(); 

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

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