简体   繁体   English

为什么 splice 不从我的数组中删除元素?

[英]Why is splice not removing elements from my array?

I am creating a roulette game that displays random items from different arrays when the wheel lands on a specific category.我正在创建一个轮盘游戏,当轮盘落在特定类别时,它会显示来自不同 arrays 的随机项目。 So far everything works except when the wheel lands on a category, it selects the same random item from the correct array over and over again.到目前为止,一切正常,除了当轮子落在一个类别上时,它会一遍又一遍地从正确的数组中选择相同的随机项目。 I am trying to use math.random and the splice method to randomly select an item from an array, and remove that item so only new, random items from the array can be displayed after, but it hasn't worked.我正在尝试使用 math.random 和 splice 方法从数组中随机 select 一个项目,然后删除该项目,以便之后只能显示数组中的新随机项目,但它没有工作。

let deg = 0;
  // The 360 degree wheel has 8 zones, so each zone is 45 degrees
  let zoneSize = 45;

const a = ["a", "b", "c", "d", "e"]
const b = ["f", "g", "h", "i", "j"]
const c = ["a", "b", "c", "d", "e"]
const d = ["f", "g", "h", "i", "j"]
const e = ["a", "b", "c", "d", "e"]
const f = ["f", "g", "h", "i", "j"]
const g = ["a", "b", "c", "d", "e"]
const h = ["f", "g", "h", "i", "j"]

// zones for each 8 categories
const symbolZones = [];
symbolZones[1] = a.splice(Math.floor(Math.random()*a.length), 1);
symbolZones[2] = b.splice(Math.floor(Math.random()*b.length), 1);
console.log(symbolZones);
symbolZones[3] = c.splice(Math.floor(Math.random()*c.length), 1);
symbolZones[4] = d.splice(Math.floor(Math.random()*d.length), 1);
console.log(symbolZones);
symbolZones[5] = e.splice(Math.floor(Math.random()*e.length), 1);
symbolZones[6] = f.splice(Math.floor(Math.random()*f.length), 1);
console.log(symbolZones);
symbolZones[7] = g.splice(Math.floor(Math.random()*g.length), 1);
symbolZones[8] = h.splice(Math.floor(Math.random()*h.length), 1);
console.log(symbolZones);


// display a question from the winning category
  const handleWin = (actualDeg) => {
    const winningSymbolNr = Math.ceil(actualDeg / zoneSize);
    display.innerHTML = symbolZones[winningSymbolNr];
  }

I don't know how the rest of the code is, but if you always get the same value over and over again it could be that you're not re-running the code that gets the values.我不知道代码的 rest 是怎样的,但如果你总是一遍又一遍地得到相同的值,那可能是你没有重新运行获取这些值的代码。

Try wrapping the logic for retrieving the symbolZones in a function:尝试将检索符号区域的逻辑包装在 function 中:

function getSymbolZones() {

  const symbolZones = [];

  symbolZones[1] = a[Math.floor(Math.random()*a.length)];
  symbolZones[2] = b[Math.floor(Math.random()*b.length)];
  symbolZones[3] = c[Math.floor(Math.random()*c.length)];
  symbolZones[4] = d[Math.floor(Math.random()*d.length)];
  symbolZones[5] = e[Math.floor(Math.random()*e.length)];
  symbolZones[6] = f[Math.floor(Math.random()*f.length)];
  symbolZones[7] = g[Math.floor(Math.random()*g.length)];
  symbolZones[8] = h[Math.floor(Math.random()*h.length)];

  return symbolZones;
}

and then use it in the handleWin function.然后在handleWin function中使用。

const handleWin = (actualDeg) => {
  const symbolZones = getSymbolZones();
  const winningSymbolNr = Math.ceil(actualDeg / zoneSize);
  display.innerHTML = symbolZones[winningSymbolNr];
}

PS I understand that you start the array at index 1 because that's the smallest zone you can get from the operation 45 / 45. But if I were you I'd start the index from 0 and just subtract 1 when accessing the array: symbolZones[winningSymbolNr - 1] . PS我知道你从索引 1 开始数组,因为这是你可以从操作 45 / 45 中获得的最小区域。但如果我是你,我会从 0 开始索引并在访问数组时减去 1: symbolZones[winningSymbolNr - 1] This way you don't get weird bugs when trying to loop through an array that has an empty first index.这样,当您尝试遍历具有空第一个索引的数组时,您就不会遇到奇怪的错误。

Other than using array , I would suggest you use object and a for loop to make the code easier to execute.除了使用array ,我建议你使用object和 for 循环来使代码更容易执行。 This code should work:此代码应该可以工作:

 let deg = 0; // The 360 degree wheel has 8 zones, so each zone is 45 degrees let zoneSize = 45; let symbol = { a: ["a", "b", "c", "d", "e"], b: ["f", "g", "h", "i", "j"], c: ["a", "b", "c", "d", "e"], d: ["f", "g", "h", "i", "j"], e: ["a", "b", "c", "d", "e"], f: ["f", "g", "h", "i", "j"], g: ["a", "b", "c", "d", "e"], h: ["f", "g", "h", "i", "j"], } // zones for each 8 categories const arr = []; for (let i in symbol) { let item = symbol[i].splice(Math.floor(Math.random() * symbol[i].length), 1) arr.push(item) } //Make the nested array become flat array let symbolZones = arr.flat(1) console.log(symbolZones) const handleWin = (actualDeg) => { const winningSymbolNr = Math.ceil(actualDeg / zoneSize); display.innerHTML = symbolZones[winningSymbolNr]; }

I rearranged the input arrays into an array of arrays (9 arrays of 5 elements = 45).我将输入 arrays 重新排列到 arrays 数组中(9 个 arrays 的 5 个元素 = 45)。 I'm guessing that you want the whole thing shuffled.我猜你想要整件事洗牌。

 const log = data => console.log(JSON.stringify(data)); let zones = [ ["", "", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", "", ""] ]; let zoneSize = zones.length * zones[0].length; let symbolZones = []; for (let i = zoneSize; i > 0; i--) { let deg = zones.flatMap(z => z.splice(Math.floor(Math.random() * z.length), 1)); if(deg.length > 0) { symbolZones.push(deg); } } log(zones); log(symbolZones);
 .as-console-row-code { display: block; width: 100%; overflow-wrap: anywhere; }.as-console-row::after { content: ''!important }

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

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