1

I'm trying to filter an array of objects using the includes method but I think I'm doing something wrong. Could someone help me? It doesn't need to be an include method but it must return objects as the example below:

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

3 Answers 3

1

You can use Array#some along with Array#includes to check if one array contains any of the elements of another array.

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 

Sign up to request clarification or add additional context in comments.

Comments

0

You can filter the item category and check if returned array length is greater than 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>

Comments

0

If you're looking for an exact match, you can use JSON.stringify to convert array to string and use === operator for matching.

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);

This will work when elements are in same position for both arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.