简体   繁体   English

如何在递归之前从数组中删除某个元素?

[英]How do I remove a certain element from my array just before the recursion?

So I'm trying to make a basic bomb wire cutting game where you get 4 randomly picked colors from an array of 6 possible colors, but I'm having trouble with getting it so there are no duplicate colors in the 4 random colors. So I'm trying to make a basic bomb wire cutting game where you get 4 randomly picked colors from an array of 6 possible colors, but I'm having trouble with getting it so there are no duplicate colors in the 4 random colors. I'm not sure how to remove already picked colors from the array.我不确定如何从阵列中删除已经选择的 colors。 I think my logic is wrong.我认为我的逻辑是错误的。

const colors = ['Blue', 'Red', 'Yellow', 'White', 'Black', 'Green']
function getXRandomColors(colors, x, result) {
    if (x === 0) return result
    const randomIndex = Math.floor(Math.random() * colors.length)
    if (colors.includes(randomIndex)){
        colors = colors.filter(color => color !== randomIndex)}
    return getXRandomColors(colors, x - 1, [...result, colors[randomIndex]])
}
var fourRandomColors = getXRandomColors(colors, 4, [])
console.log('The wires are: ' + fourRandomColors);

I'd create a temporary variable set to the picked color, then removing that array entry at the randomIndex before continuing recursion.我会创建一个临时变量设置为选择的颜色,然后在继续递归之前在randomIndex删除该数组条目。

const colors = ['Blue', 'Red', 'Yellow', 'White', 'Black', 'Green']
function getXRandomColors(colors, x, result) {
    if (x === 0) return result
    const randomIndex = Math.floor(Math.random() * colors.length)
    let pickedColor = colors[randomIndex];
    colors.splice(randomIndex, 1);
    return getXRandomColors(colors, x - 1, [...result, pickedColor])
}
var fourRandomColors = getXRandomColors(colors, 4, [])
console.log('The wires are: ' + fourRandomColors);

This way you are guaranteed there won't be any duplicates.这样你就可以保证不会有任何重复。

Couple things that are going wrong in your current example:在您当前的示例中出现了几个问题:

In your filter function在你的过滤器 function

    colors = colors.filter(color => color !== randomIndex)}

The condition ( color !== randomIndex ) will always be true, because random index is a number and color is a string.条件( color !== randomIndex )将始终为真,因为随机索引是一个数字,而颜色是一个字符串。 What you'd want to do if you'd compare index is;如果您比较索引,您想要做的是;

    colors = colors.filter((color, index) => index !== randomIndex)}

But even this won't have the desired result , since you're removing the random index from your array without storing it somewhere, this is how duplicates will show up in the first place.但即使这样也不会得到想要的结果,因为您要从数组中删除随机索引而不将其存储在某处,这就是重复项首先出现的方式。

暂无
暂无

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

相关问题 如果每个值有多个,我如何根据其值从数组中仅删除一个元素 - How do I remove just one element from my array based on its value if there are more than one of each value 在javascript中,如何从对象数组中删除元素? - In javascript, how do I remove an element from an array of objects? 我可以在 javascript 中任何数组的某个元素之前返回一个值吗? - Can I return a value just before a certain element of any array in javascript? 如何在某些条件下从元素中删除Bootstrap Affix? - How do I remove Bootstrap Affix from an element under certain conditions? 如何从我的列表中删除某些元素 Javascript html - How to remove certain element from my list Javascript html 如何编写代码以从数组中删除一个元素并在其位置插入另一个元素 - How do I write a code to remove the an element from an array and insert another element in its place 如何从javascript中的数组中删除某些匹配条件的元素? - How do I remove certain elements matching criteria from an array in javascript? 如何从阵列中删除数组? - How do I remove an array from an array? 如何使用Array.filter()函数从数组中删除单个元素? - How do I use the Array.filter() function to remove a single element from an array? Material React Select Multiple:单击选定元素时如何从数组中删除? - Material React Select Multiple: How i can remove from my array when i click in a selected element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM