【发布时间】:2018-02-09 10:53:55
【问题描述】:
首先,请原谅我的英语。我会尽量解释清楚。
我想开发一个多米诺骨牌主机游戏。
我将所有多米诺骨牌存储到一个名为 dominoes 的数组中。
function createNewDominoes(dominoes) {
newDomino = {
a: Math.floor((Math.random() * 7) - 0),
b: Math.floor((Math.random() * 7) - 0),
tipo: null
}
newDominoFlipped = {
a: nuevaFicha.b,
b: nuevaFicha.a,
tipo: null
}
console.log(nuevaFicha, nuevaFichaVolteada)
if ((includes(dominoes, newDomino)) || (includes(dominoes, newDominoFlipped))) {
console.log("Element already exist")
} else {
array.push(nuevaFicha)
}
}
我使用的是 Node.js,我在 app.js 文件中包含了 array-includes 模块,这样我就可以检查我的数组的元素。
问题是,当我得到数组时,函数返回 28 个元素,这没关系,但有些是重复的。示例:
[ { a: 3, b: 0, tipo: null },
{ a: 5, b: 2, tipo: null },
{ a: 1, b: 4, tipo: null },
{ a: 1, b: 6, tipo: null },
{ a: 2, b: 4, tipo: null },
{ a: 4, b: 2, tipo: null },
{ a: 4, b: 2, tipo: null },
{ a: 3, b: 3, tipo: null },
{ a: 5, b: 5, tipo: null },
{ a: 3, b: 4, tipo: null },
{ a: 0, b: 6, tipo: null },
{ a: 6, b: 0, tipo: null },
{ a: 4, b: 4, tipo: null },
{ a: 6, b: 3, tipo: null },
{ a: 5, b: 2, tipo: null },
{ a: 3, b: 4, tipo: null },
{ a: 3, b: 5, tipo: null },
{ a: 2, b: 3, tipo: null },
{ a: 3, b: 4, tipo: null },
{ a: 3, b: 3, tipo: null },
{ a: 5, b: 3, tipo: null },
{ a: 0, b: 1, tipo: null },
{ a: 2, b: 2, tipo: null },
{ a: 0, b: 3, tipo: null },
{ a: 2, b: 3, tipo: null },
{ a: 4, b: 4, tipo: null },
{ a: 0, b: 4, tipo: null },
{ a: 5, b: 1, tipo: null } ]
我需要得到所有 28 张多米诺骨牌,但它们不能重复或翻转重复。
希望你能帮助我
【问题讨论】:
-
你能把你所有的代码都粘贴过来吗!
-
我会根据你的变量名做一个假设,如果我弄错了,很抱歉,但是有一个Stack Overflow en español。
-
似乎
array-includes并没有完全按照你的想法做 -
在 Javascript 中,
{a:1, b:2} !== {a:1, b:2}- 所以includes不是你想要的
标签: javascript arrays node.js object