将 OR 条件添加到筛选器不会返回任何结果



我正在使用filter,在map之前创建一个树状结构。

使用以下代码片段,它正确地向我返回了我想要的数据:

const nest = (tree, cod = basenode, link = 'link_id') =>
(tree.filter(item => (item[link].toUpperCase() == cod.toUpperCase())).map(item => ({ ...item, productstructures: [{ operations: [{ materials: nest(tree, item.cod) }] }] })));

但是,将 OR 条件添加到filter函数时,它不会返回任何结果。至少OR条件不应该给我完全相同的结果吗?

const nest = (tree, cod = basenode, link = 'link_id') =>
(tree.filter(item => (item[link].toUpperCase() == cod.toUpperCase())||(item[link]=="")).map(item => ({ ...item, productstructures: [{ operations: [{ materials: nest(tree, item.cod) }] }] })));

提前感谢!

括号不匹配 使用这个,让我知道它是否有帮助。

const nest = (tree, cod = basenode, link = 'link_id') =>
(tree.filter(item =>( (item[link].toUpperCase() == cod.toUpperCase()) ||(item[link]=="") ) ).map(item => ({ ...item, productstructures: [{ operations: [{ materials: nest(tree, item.cod) }] }] })));

它应该可以工作 就像,我尝试使用一些虚拟对象,例如

let tree= [{abc:1,cd:2},{abc:2,cd:2},{abc:1,cd:3},{abc:3,cd:3}]
console.log(tree.filter(item => (item.abc==1 || item.abc== 2)))

最新更新