【问题标题】:How can I know when a object is already into my array我怎么知道一个对象何时已经进入我的数组
【发布时间】: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 张多米诺骨牌,但它们不能重复或翻转重复。

希望你能帮助我

【问题讨论】:

标签: javascript arrays node.js object


【解决方案1】:

您必须创建自定义函数来检查重复项。每次创建新对象时,它永远不会满足 Array.indexOf() 函数。你可以这样写一个函数:

function containsItemOrFlippedItem(list, item) {
    return list.some(function(current) {
        return (current.a === item.a && current.b === item.b)
            || (current.a === item.b && current.b === item.a);
    });
}

function createNewDominoes(dominoes) {
    var newDomino = {
        a: Math.floor((Math.random() * 7) - 0),
        b: Math.floor((Math.random() * 7) - 0),
        tipo: null
    };
    if (containsItemOrFlippedItem(dominoes, newDomino)) {
        console.log("Element already exist");
    } else {
        dominoes.push(newDomino);
    }
}

这里我使用了Array的some()函数。在MDN 上查看some() 的文档。希望对你有帮助!!

【讨论】:

  • 哇,这对我帮助很大,我不知道 some() 函数。谢谢
  • @Enrique 该解决方案是否帮助您并解决了您的问题?如果是这样,您可以将其标记为已接受的答案。谢谢!!
【解决方案2】:

编写自己的函数来查看多米诺骨牌是否相等。然后使用Array.some 方法查看是否有任何多米诺骨牌满足该条件:

const arr = [{ a: 3, b: 0, tipo: null }]

const same = {a: 3, b: 0, tipo: null};
const reverse = {a: 0, b: 3, tipo: null};
const diff = {a: 4, b: 0, tipo: null};

function dominoEqual(d1, d2) {
    if (d1.tipo !== d2.tipo) return false;
    return (d1.a === d2.a && d1.b === d2.b) || (d1.a === d2.b && d1.b === d2.a);
}

console.log(arr.some(d => dominoEqual(d, same))) // repeated
console.log(arr.some(d => dominoEqual(d, reverse))) // repeated but reversed
console.log(arr.some(d => dominoEqual(d, diff))) // not repeated

然后使用它而不是 includes 来检查重复项:

if (dominos.some(d => dominoEqual(d, newDomino))) {
    console.log("Element already exist")
} else {
    array.push(nuevaFicha)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    相关资源
    最近更新 更多