使用包含数组值的过滤器数组
Filter array using includes with array values
我正在尝试使用 includes
方法过滤对象数组,但我认为我做错了什么。有人可以帮我吗?
它不需要是包含方法,但它必须 return 个对象,如下例所示:
<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>
您可以使用 Array#some
和 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));
})
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
您可以过滤项目类别并检查返回的数组长度是否大于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>
如果您正在寻找完全匹配,您可以使用 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)=>{
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);
这将在两个数组的元素处于相同位置时起作用。
我正在尝试使用 includes
方法过滤对象数组,但我认为我做错了什么。有人可以帮我吗?
它不需要是包含方法,但它必须 return 个对象,如下例所示:
<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>
您可以使用 Array#some
和 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));
})
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
您可以过滤项目类别并检查返回的数组长度是否大于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>
如果您正在寻找完全匹配,您可以使用 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)=>{
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);
这将在两个数组的元素处于相同位置时起作用。