这将返回一个空数组。我也尝试过使用selectedRules[0]
本身的数组,但是得到了相同的结果。如何将对象数组中的过滤器与字符串数组进行比较,以使用每个过滤后的规则返回对象数组?
const allRules = [
{
RuleName: "Two",
RuleId: 2
},
{
RuleName: "Three",
RuleId: 3
},
{
RuleName: "Four",
RuleId:4
}
];
const selectedRules = ["2", "3"]
const filteredRule = allRules.filter(x => x.RuleId === selectedRules)
console.log(filteredRule) // []
答案 0 :(得分:0)
您需要
===
成为数字)selectedRules
数组.includes
的值RuleId
是否被迭代:
const allRules = [{
RuleName: "Two",
RuleId: 2
},
{
RuleName: "Three",
RuleId: 3
},
{
RuleName: "Four",
RuleId: 4
}
];
const selectedRules = ["2", "3"]
const filteredRule = allRules.filter(x => selectedRules.includes(String(x.RuleId)))
console.log(filteredRule)