我希望其ID,名称,电子邮件或全部三个与用户给出的查询匹配的用户。一切似乎都正常,但是map函数..有人可以向我解释我在这里做错了吗...
import {UserModel} from '../user.model';
export class UserFilterService {
users: UserModel[] = [];
constructor() {
}
searchUser(users: UserModel[], id, name, email): UserModel[] {
this.users = users;
let reqUsers;
if (id !== '' || name !== '' || email !== '') {
reqUsers = this.searchByAll(id, name, email);
return reqUsers;
}
}
searchByAll = (id, name, email) => {
const reqUser: UserModel[] = this.users.map((user) => {
if (user.getId() === id && user.getName() === name && user.getEmail() === email) {
return user;
}
else if (user.getId() !== id && user.getName() === name && user.getEmail() === email ) {
return user;
}
else if (user.getId() === id && user.getName() === name && user.getEmail() !== email) {
return user;
}
else if (user.getId() === id && user.getName() !== name && user.getEmail() === email) {
return user;
}
else if (user.getId() === id && user. getName() !== name && user.getEmail() !== email) {
return user;
}
else if (user.getId() !== id && user.getName === name && user.getEmail() !== email) {
return user;
}
else if (user.getId() !== id && user.getName() !== name && user.getEmail() === email) {
return user;
}
});
console.log(reqUser);
return reqUser;
}
}
答案 0 :(得分:0)
我将searchByAll方法更新为
searchByAll = (id, name, email) => {
const reqUser: UserModel[] = this.users.filter((user) => user.getId() === id || user.getName() === name || user.getEmail() === email);
console.log(reqUser);
return reqUser;
}
成功了