更新时间:2023-11-30 20:01:52
您可以创建一个 counter
对象,该对象将每个 randomNum
作为键及其次数显示为值.然后,使用 Object.values()
和
You could create a counter
object which will have each randomNum
as key and the number of times it appears as value. Then, check if the counter
has 4 as value using Object.values()
and includes
const counter = diceArr.reduce((acc, o) => {
acc[o.randomNum] = acc[o.randomNum] + 1 || 1;
return acc
}, {})
const isFourOfAKind = Object.values(counter).includes(4);
console.log(isFourOfAKind)