有没有更好的方法根据特定属性的一组可能值来过滤对象数组?
这种方法可行但似乎有点笨重。
在 Python 中,它可能是这样的:
这种方法可行但似乎有点笨重。
const all = [
{id: 1}, {id: 2}, {id: 3} ...
]
const ids = [2, 3];
const selected = (all, ids) =>
all.filter(obj => ids.map(id => id === obj.id).some(match => match));
selected(all, ids);
// [{id: 2}, {id: 3}]
在 Python 中,它可能是这样的:
selected = [obj for obj in all if obj.id in ids]
all.filter(({id}) => ids.includes(id))
。 - ibrahim mahrir