【study】每次选中的值,弄在一个数组里面,并去重

55 阅读1分钟
 selectedUserInfo: [], // 定义选中的值
 
 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) => {
                            // eslint-disable-next-line no-unused-expressions
                            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);
        },