有个联动的标签选择(如图)
例如:第一次选择了(["麻辣拌(无汤带麻酱)", "酸甜麻辣拌(无麻酱)"])两个属性后,如果取消了其中一个属性,那根据我的条件判断,应该是替换相应的值,而不是新增。现在出现的问题是,每次根据type类型查找下标,每次都是-1。
代码
checkAttribute: function (e) {
let pre = 0;
let obj = {};
let tempArr = this.pureData.tempArr;
const values = e.detail.value;
console.log("values(1386) => ", values);
const types = this.data.curAttributesType;
const curAttributes = this.data.curAttributes;
curAttributes.forEach(item => {
const isExist = values.some(cur => cur === item.name)
if (isExist) {
item.checked = true;
} else {
item.checked = false;
}
});
const myAtr = curAttributes.filter(item => item.checked === true);
obj.type = types;
obj.value = myAtr;
if (this.pureData.tempArr.length > 0) {
const typeIdx = tempArr.findIndex(item => { item.type === this.data.curAttributesType });
console.log("typeIdx(1404) => ", typeIdx);
if (typeIdx != -1) {
this.pureData.tempArr[`${typeIdx}`].value = myAtr;
} else {
this.pureData.tempArr.push(obj)
}
} else {
this.pureData.tempArr.push(obj);
}
console.log("tempArr(1412) => ", this.pureData.tempArr);
this.setData({
curAttributes: curAttributes,
});
},
const typeIdx = tempArr.findIndex(item => { return item.type === this.data.curAttributesType });