vue selectionRows数组中数据字段key是不重复的但是可以多次赋值给thismustMiddlegroundDifArr数组如何避免thismustMiddlegroundDifArr数组中数据不会重复请给出完整代码
以下是一个示例代码,演示如何避免向数组中添加重复的数据:
export default {
data() {
return {
selectionRows: [
{ key: 1, value: 'A' },
{ key: 2, value: 'B' },
{ key: 3, value: 'C' }
],
mustMiddlegroundDifArr: []
};
},
methods: {
addToMustMiddlegroundDifArr(row) {
// 检查this.mustMiddlegroundDifArr数组是否已经包含了相同的key值
const isDuplicate = this.mustMiddlegroundDifArr.some(item => item.key === row.key);
if (!isDuplicate) {
this.mustMiddlegroundDifArr.push(row);
}
}
}
};
在这个示例中,我们使用Array.prototype.some()方法来检查this.mustMiddlegroundDifArr数组中是否已经包含了相同的key值。只有当this.mustMiddlegroundDifArr数组中不存在相同的key值时,才将row添加到this.mustMiddlegroundDifArr数组中。这样就可以确保this.mustMiddlegroundDifArr数组中的数据不会重复
原文地址: https://www.cveoy.top/t/topic/iE1Y 著作权归作者所有。请勿转载和采集!