我想在嵌套的對象數組中找到一個數組,我該怎么做?這是我的數組const arr = [ { "teamA": [ { "_id": "5fcb57c5a1a426c03bcfd25f", "level": 10, "username": "asaf" } ], "teamB": [], "options": {} }, { "teamA": [ { "_id": "a7fgy3h1uio", "level": 10, "username": "asaf" } ], "teamB": [ { "_id": "13rfedsc32", "level": 10, "username": "asaf" }, { "_id": "dghg453r3q", "level": 10, "username": "asaf" } ], "options": {} }];現在我想創建一個函數,通過 _id 返回玩家所在團隊的數組例如,我創建了這個:const findTeam = playerId => { const match = arr.find(({ teamA, teamB }) => [teamA, teamB].some(team => team.some(i => i._id == playerId))); if(!match) return; const { teamA, teamB } = match; const team = [teamA, teamB].find(team => team.some(i => i._id == playerId)); return team;};它正在工作,但是我這樣做的方式看起來很混亂,有什么巧妙的方法可以做到這一點嗎?謝謝!
在嵌套的對象數組中查找數組
牛魔王的故事
2023-10-14 17:04:04