我有users
的列表:
users: Array<any> = [{id:1,name:'A'},{id:2,name:'B'},{id:3,name:'C'}];
和
this.selectedUsers = [1,2];
如何从users
阵列中找到id
数组中找到selectUsers
的所有对象?
答案 0 :(得分:2)
您可以将indexOf与过滤器一起使用。
this.users.filter(user => this.selectedUsers.indexOf(user.id ) > -1)
答案 1 :(得分:0)
我认为这应该做你想要的:
var found = this.users.filter(
item => this.selectedUsers.some(id => item.id == id)
)
<强>更新强>
var found = this.users.filter(
item => this.selectedUsers.some(id => item.id == id)
).forEach(item => {
item.label = item.name;
item.name = undefined;
});
或
var found = this.users.filter(
item => this.selectedUsers.some(id => item.id == id)
).map(item => {id: item.id, label: item.name});