我有一个对象数组,如下所示:第一个月我已经定义了联合类型type NamesType = 'something' | 'bob'可以通过类型过滤数组吗?所以最终结果是array = [{name: 'something'}, {name: 'bob'}]
type NamesType = 'something' | 'bob'
array = [{name: 'something'}, {name: 'bob'}]
huwehgph1#
您可以执行以下操作:
array = [{name: 'something'}, {name: 'random'}, {name: 'bob'}]; namesType = ['something','bob']; filteredArray = array.filter(item => namesType.includes(item.name));
ni65a41a2#
一个示例可以像这样使用Set:
Set
const array = [{name: 'something'}, {name: 'random'}, {name: 'bob'}] const namesType = new Set(['something','bob']); const filteredArray = array.filter(item => namesType.has(item.name)); console.log(filteredArray)
2条答案
按热度按时间huwehgph1#
您可以执行以下操作:
ni65a41a2#
一个示例可以像这样使用
Set
: