首页 > 解决方案 > 如果数据库中已存在用户,如何使用 Math.random() 将其排除在随机之外

问题描述

作为学校项目的一部分,我们正在创建一个应用程序,如果他们玩相同的游戏,用户可以在其中相互匹配。当您单击 matchButton 时,一个玩您选择的游戏的随机用户将显示在页面上。您可以选择喜欢该用户或继续点击 matchButton 以随机化另一个用户。

现在我们要确保您已经喜欢的用户不会被随机化,因此不会在页面上呈现。

对于每个用户,被点赞的用户都保存在数据库中名为 match 的数组中。玩相同游戏的用户保存在一个名为 numOfMatches 的数组中。因此,我们使用 numOfMatches 来随机化用户。

我想我必须遍历数据库中的 numOfMatches 和匹配数组,以查看 numOfMatches 中的用户是否已经在数据库中。如果是这样,我需要从 numOfMatches 中删除用户,以便它不包含在 Math.random 代码中。

我在这篇文章的最后一个函数中尝试使用此代码,但在理解如何访问用户对象中匹配数组中的元素以及如何使用拼接(以及我应该将代码放置在哪个函数中)时遇到了问题我们使用回调,我认为这有点棘手):

for(let i = 0; i < numOfMatches.length; i++) {
    for(let j = 0; j < users[j].match.length; j++) {
        if(numOfMatches[i] === users[j].match) {
          let position = numOfMatches.indexOf(randomUser)
             numOfMatches.splice(position, 1)
        }
    }
}

我非常感谢您的想法和建议。

来自数据库的样本:

{
   "username":"bobox",
   "email":"bobox@hotmail.com",
   "password":"test234",
   "gender":"Man",
   "age":"17",
   "city":"Jönköping",
   "games":"Battlefield V",
   "usernameDiscord":"bigbox",
   "usernameSteam":"bigbox",
   "usernameOrigin":"bobox",
   "match":[
      "carro",
      "arooma",
      "gamer_girl"
   ],
   "_id":"WRa86pRsVex6NBe2"
}

function renderMatches(users) {
    let bigDiv = document.querySelector(".TheBigDiv")
    let matches = document.querySelector(".Match__List")
    let matchGames = document.querySelector(".Match__Games")
    let matchButton = document.querySelector(".Match__Button")
    let noMatch = document.createElement("h3")

    matchButton.addEventListener("click", async(event) => {
        hanna(users)
    })
}

function randomMatches(numOfMatches, users) {
    let bigDiv = document.querySelector(".TheBigDiv")
    let matches = document.querySelector(".Match__Lis")
    console.log("numOfMatches:", numOfMatches)

    bigDiv.innerHTML = ""
    console.log("Funkar här")
    let newClone = matches.cloneNode(true)
    let randomUser = numOfMatches[Math.floor(Math.random() * numOfMatches.length)]

    newClone.querySelector('.Match-Username').innerHTML = randomUser.username
    newClone.querySelector('.Match-Age').innerHTML = randomUser.age
    newClone.querySelector('.Match-Game').innerHTML = "Spelar:" + " " + randomUser.games
    newClone.classList.remove("Prototype")
    bigDiv.append(newClone)

    likeUser(randomUser, users)
}


function hanna(users) {
    console.log(users)
    let numOfMatches = []
    let matchGames = document.querySelector(".Match__Games")
    let matches = document.querySelector(".Match__Lis")
    let bigDiv = document.querySelector(".TheBigDiv")
    let noMatch = document.createElement("h3")
    for (let j = 0; j < users.length; j++) {
        let currentUser = users[j]
        let UserID = currentUser._id
        let gejm = matchGames.querySelector(".gejms").value
        if (currentUser.games == gejm) {
            console.log("users:", currentUser)
            console.log(currentUser._id)
            numOfMatches.push(currentUser)
            console.log("hej")
            randomMatches(numOfMatches, users)

        }
    }
    if (numOfMatches.length == 0) {
        noMatch.innerHTML = "No matches found"
        bigDiv.append(noMatch)
    }

}

标签: javascriptarraysdatabaseloopsobject

解决方案


这是一个简单的例子。

const candidate = [1,2,3,4,5,6,7,8,9,0]
const exclude = [1,3,7]

let target = candidate.filter(x=>!exclude.includes(x))
console.log(target)

console.log(target[Math.floor(Math.random() * target.length)])


推荐阅读