所以我试图在对象数组上应用一些过滤器。例如,我有两个对象:
[
{_id: "5e8e07f1b874fd27906b2f6a", index: "00004000003", manufacturer: "ABS", plasticType: "new16", color: "Red", …},
{_id: "5e90e60c1485d58d88f0af6f", index: "00003000004", manufacturer: "ABS", plasticType: "new15", color: "Red", …}
]
我有适用于制造商,plasticType和颜色的过滤器。因此,如果我只想查看制造商为ABS
,plasticType为new16
,颜色为Red
的对象,则需要应用一些过滤器。
我该怎么办?
预期结果应仅返回第一个对象。
我已经尝试过了,但是很明显,如果所有条件都匹配,它只会返回正确的结果。
data.filter((spool) =>
filters.manufacturers.includes(spool.manufacturer) &&
filters.plasticTypes.includes(spool.plasticType) &&
filters.colors.includes(spool.color)
)
答案 0 :(得分:0)
您可以通过以下方法解决它:
const filteredData = data.filter(el => el.manufacturer.includes("ABS") &&
el.plasticType.includes('new16') && el.color.includes('Red'))
或
const filteredData = data.filter(el => el.manufacturer == "ABS" && el.plasticType ==
'new16' && el.color == 'Red')