第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在嵌套的對象數(shù)組中查找數(shù)組

在嵌套的對象數(shù)組中查找數(shù)組

牛魔王的故事 2023-10-14 17:04:04
我想在嵌套的對象數(shù)組中找到一個數(shù)組,我該怎么做?這是我的數(shù)組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": {}    }];現(xiàn)在我想創(chuàng)建一個函數(shù),通過 _id 返回玩家所在團(tuán)隊的數(shù)組例如,我創(chuàng)建了這個: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;};它正在工作,但是我這樣做的方式看起來很混亂,有什么巧妙的方法可以做到這一點(diǎn)嗎?謝謝!
查看完整描述

1 回答

?
富國滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個贊

您可以使用flatMap

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:{}}];


const findTeam = playerId => arr.flatMap(({ teamA, teamB }) => [teamA, teamB])

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .find(team => team.some(player => player._id === playerId));


console.log(findTeam('13rfedsc32'));



查看完整回答
反對 回復(fù) 2023-10-14
  • 1 回答
  • 0 關(guān)注
  • 88 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號