【发布时间】:2021-04-29 17:27:24
【问题描述】:
我正在尝试构建一个包含各种情绪的情绪范围滑块:
当滑块改变时,它会添加或删除情绪变化为一系列情绪:
被添加或删除的条件是:
- 如果
selectedEmotion数组为空,则将emotion推入数组 - 如果
emotion不在selectedEmotions[]中,则推送到selectedEmotions - 如果
emotion在selectedEmotions[]中,请勿添加重复项 - 如果
emotion.intensity归零,则从selectedEmotions[]中删除emotion
功能
buildEmotions(index, intensity) {
//If selected is empty
if (this.selectedEmotions.length >= 1) {
//Loop through selected emotions and check for new value
this.selectedEmotions.forEach(
function (emotion, i) {
//If not included in selected emotions push to array
if (emotion.emotion_id !== this.emotions[index].emotion_id) {
if (emotion.intensity) {
this.selectedEmotions.push({
emotion_id: this.emotions[index].emotion_id,
intensitey: this.emotions[index].intensity,
newIntensity: this.emotions[index].newIntensity
});
} else {
console.log(this.selectedEmotions[i]);
this.selectedEmotions.splice(i, 1);
}
}
}.bind(this)
);
//Push new emotion to array if there are no emotions in selectedEmotions
} else {
this.selectedEmotions.push({
emotion_id: this.emotions[index].emotion_id,
intensitey: this.emotions[index].intensity,
newIntensity: this.emotions[index].newIntensity
});
}
}
Here's the full codepen example
我遇到的问题是范围滑块会添加重复项,并随机删除emotions。它似乎适用于第一种情绪,但在选择第二个之后就失败了。
【问题讨论】:
-
在哪里添加了重复项?你能在这里提供一些更强大的可重复步骤吗?例如:当您执行
X时,就会发生这种情况。但你预计会发生其他事情。对我来说,代码就像写的那样工作。
标签: javascript arrays vue.js vuetify.js