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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

JS:過(guò)濾對(duì)象列表以僅包含在第二個(gè)數(shù)組中找到的字符串

JS:過(guò)濾對(duì)象列表以僅包含在第二個(gè)數(shù)組中找到的字符串

慕少森 2022-09-29 10:24:04
我有一個(gè)類似于以下內(nèi)容的對(duì)象數(shù)組:let array = [{id: 1,name: Foo,tools: [{id:3, toolname: Jaw},{id:1, toolname: Law}]},{id: 2,name: Boo,tools: [{id:2, toolname: Caw}]},{id: 3,name: Loo,tools: [{id:3, toolname: Jaw}, {id:4, toolname: Maw}, {id:6, toolname: Taw}]}]我有第二個(gè)數(shù)組,如下所示:let secondarray = ['Jaw', 'Taw']如何返回僅包含第二個(gè)數(shù)組中兩個(gè)項(xiàng)目的對(duì)象列表?因此,如果一個(gè)對(duì)象只有兩者中的一個(gè),則不會(huì)包含該對(duì)象。此外,如果對(duì)象包含其他不在 中,但同時(shí)包含 兩項(xiàng),則仍應(yīng)包含它。toolssecondarraysecondarray因此,此濾波器的預(yù)期輸出為:{id: 3,name: Loo,tools: [{id:3, toolname: Jaw}, {id:4, toolname: Maw}, {id:6, toolname: Taw}]}感謝您抽出寶貴時(shí)間接受采訪!
查看完整描述

1 回答

?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以迭代數(shù)據(jù)數(shù)組,然后檢查所有需要的工具是否都在實(shí)際對(duì)象中。


let array = [{ id: 1, name: 'Foo', tools: [{ id: 3, toolname: 'Jaw' }, { id: 1, toolname: 'Law' }] }, { id: 2, name: 'Boo', tools: [{ id: 2, toolname: 'Caw' }] }, { id: 3, name: 'Loo', tools: [{ id: 3, toolname: 'Jaw' }, { id: 4, toolname: 'Maw' }, { id: 6, toolname: 'Taw' }] }],

    required = ['Jaw', 'Taw'],

    result = array.filter(({ tools }) =>

        required.every(v => tools.some(({ toolname }) => toolname === v))

    );


console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }


另一種通過(guò)使用 Set 多次省略循環(huán)的方法。tools


let array = [{ id: 1, name: 'Foo', tools: [{ id: 3, toolname: 'Jaw' }, { id: 1, toolname: 'Law' }] }, { id: 2, name: 'Boo', tools: [{ id: 2, toolname: 'Caw' }] }, { id: 3, name: 'Loo', tools: [{ id: 3, toolname: 'Jaw' }, { id: 4, toolname: 'Maw' }, { id: 6, toolname: 'Taw' }] }],

    required = ['Jaw', 'Taw'],

    result = array.filter(({ tools }) => 

        required.every(

            Set.prototype.has,

            new Set(tools.map(({ toolname }) => toolname))

        )

    );


console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對(duì) 回復(fù) 2022-09-29
  • 1 回答
  • 0 關(guān)注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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