- 背景:有这么一个接口,可以返回公司员工的账号enName名字,现在需要根据enName匹配chName,接口每次根据keyWord返回模糊查询符合条件的值,并没有返回所有的员工值
- 注:解决方法,就是把每次匹配出来的值保存下来,累加
原有数据:['tanglijianxue', 'xiongda']
需要数据:
[
{enName:'tanglijianxue', chName: '棠梨煎雪'},
{enName:'xiongda', chName: '熊大'},
]
data(){
selectedUserInfo: [],
}
methods: {
change(val){
this.changeUserName(val);
},
changeUserName(val) {
if (val.length > this.selectedUserInfo.length) {
val.forEach((i) => {
const item = this.options.filter((j) => i === j.value)[0];
if (item) {
this.selectedUserInfo.push(item);
const hash = {};
this.selectedUserInfo = this.selectedUserInfo.reduce((preVal, curVal) => {
hash[curVal.id] ? ' ' : hash[curVal.id] = true && preVal.push(curVal);
return preVal;
}, []);
}
});
} else {
const arr = [];
val.forEach((i) => {
this.selectedUserInfo.forEach((j) => {
if (i === j.mis) {
arr.push(j);
}
});
});
this.selectedUserInfo = arr;
}
this.$emit('changeUser', this.selectedUserInfo);
},
}