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

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

使用包含數(shù)組值來(lái)過(guò)濾數(shù)組

使用包含數(shù)組值來(lái)過(guò)濾數(shù)組

富國(guó)滬深 2023-10-14 16:17:01
我正在嘗試使用該方法過(guò)濾對(duì)象數(shù)組includes,但我認(rèn)為我做錯(cuò)了。有人可以幫助我嗎?它不需要是一個(gè)包含方法,但它必須返回對(duì)象,如下例所示:<html><script>const testeArray = [{name:"antonio", category: ["One","Two"]},{name:"joana", category: ["Two"]}];const catArray1 = ["One","Two"];const catArray2 = ["One"];const text = "an"const resultArray1 = testeArray.filter((item)=>{return item.name.includes(text) && item.category.includes(catArray1);})  console.log(resultArray1);  //should return antonio and joana objectsconst resultArray2 = testeArray.filter((item)=>{return item.name.includes(text) && item.category.includes(catArray2);})  console.log(resultArray2);   //should return antonio object only </script></html><html><script>const testeArray = [{name:"antonio", category: ["One","Two"]},{name:"joana", category: ["Two"]}];const catArray1 = ["One","Two"];const catArray2 = ["One"];const text = "an"const resultArray1 = testeArray.filter((item)=>{return item.name.includes(text) && item.category.includes(catArray1);})  console.log(resultArray1);  //should return antonio and joana objectsconst resultArray2 = testeArray.filter((item)=>{return item.name.includes(text) && item.category.includes(catArray2);})  console.log(resultArray2);   //should return antonio object only </script></html>
查看完整描述

3 回答

?
偶然的你

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

您可以使用Array#somewith 來(lái)Array#includes檢查一個(gè)數(shù)組是否包含另一個(gè)數(shù)組的任何元素。


const testeArray = [{name:"antonio", category: ["One","Two"]},{name:"joana", category: ["Two"]}];

const catArray1 = ["One","Two"];

const catArray2 = ["One"];

const text = "an"


const resultArray1 = testeArray.filter((item)=>{

return item.name.includes(text) && item.category.some(x => catArray1.includes(x));

})  

console.log(resultArray1);  //should return antonio and joana objects


const resultArray2 = testeArray.filter((item)=>{

return item.name.includes(text) && item.category.some(x => catArray2.includes(x));

})  

console.log(resultArray2);   //should return antonio object only 


查看完整回答
反對(duì) 回復(fù) 2023-10-14
?
MMTTMM

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

如果您正在尋找精確匹配,可以使用JSON.stringify將數(shù)組轉(zhuǎn)換為字符串并使用===運(yùn)算符進(jìn)行匹配。


const testeArray = [{name:"antonio", category: ["One","Two"]},{name:"joana", category: ["Two"]}];

const catArray1 = ["One","Two"];

const catArray2 = ["One"];

const text = "an"


const resultArray1 = testeArray.filter((item)=>{

    return item.name.includes(text) && JSON.stringify(item.category) === JSON.stringify(catArray1);

})  

console.log(resultArray1);  //should return      antonio and joana objects


const resultArray2 = testeArray.filter((item)=>{

    return item.name.includes(text) && JSON.stringify(item.category) === JSON.stringify(catArray2);

})  

console.log(resultArray2);

當(dāng)兩個(gè)數(shù)組的元素位于相同位置時(shí),這將起作用。


查看完整回答
反對(duì) 回復(fù) 2023-10-14
?
aluckdog

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

您可以過(guò)濾項(xiàng)目類(lèi)別并檢查返回的數(shù)組長(zhǎng)度是否大于0。


<html>

<script>

const testeArray = [{name:"antonio", category: ["One","Two"]},{name:"joana", category: ["Two"]}];

const catArray1 = ["One","Two"];

const catArray2 = ["One"];

const text = "an"


const resultArray1 = testeArray.filter((item)=>{

return item.name.includes(text) && item.category.filter(cat => catArray1.indexOf(cat) > -1).length > 0;

})  

console.log(resultArray1);  //should return antonio and joana objects


const resultArray2 = testeArray.filter((item)=>{

return item.name.includes(text) && item.category.filter(cat => catArray2.indexOf(cat) > -1).length > 0;

})  

console.log(resultArray2);   //should return antonio object only 


</script>

</html>


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

添加回答

舉報(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)