猿问

使用包含数组值来过滤数组

我正在尝试使用该方法过滤对象数组includes,但我认为我做错了。有人可以帮助我吗?它不需要是一个包含方法,但它必须返回对象,如下例所示:


<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 objects


const 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 objects


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

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

})  

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


</script>

</html>


富国沪深
浏览 118回答 3
3回答

偶然的你

您可以使用Array#somewith 来Array#includes检查一个数组是否包含另一个数组的任何元素。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));})&nbsp;&nbsp;console.log(resultArray1);&nbsp; //should return antonio and joana objectsconst resultArray2 = testeArray.filter((item)=>{return item.name.includes(text) && item.category.some(x => catArray2.includes(x));})&nbsp;&nbsp;console.log(resultArray2);&nbsp; &nbsp;//should return antonio object only&nbsp;

MMTTMM

如果您正在寻找精确匹配,可以使用JSON.stringify将数组转换为字符串并使用===运算符进行匹配。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)=>{&nbsp; &nbsp; return item.name.includes(text) && JSON.stringify(item.category) === JSON.stringify(catArray1);})&nbsp;&nbsp;console.log(resultArray1);&nbsp; //should return&nbsp; &nbsp; &nbsp; antonio and joana objectsconst resultArray2 = testeArray.filter((item)=>{&nbsp; &nbsp; return item.name.includes(text) && JSON.stringify(item.category) === JSON.stringify(catArray2);})&nbsp;&nbsp;console.log(resultArray2);当两个数组的元素位于相同位置时,这将起作用。

aluckdog

您可以过滤项目类别并检查返回的数组长度是否大于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;})&nbsp;&nbsp;console.log(resultArray1);&nbsp; //should return antonio and joana objectsconst resultArray2 = testeArray.filter((item)=>{return item.name.includes(text) && item.category.filter(cat => catArray2.indexOf(cat) > -1).length > 0;})&nbsp;&nbsp;console.log(resultArray2);&nbsp; &nbsp;//should return antonio object only&nbsp;</script></html>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答