我有一个HTMLElements数组。我要删除重复项
const element = event.target as HTMLElement;
if (element.classList.contains('singleRowPath')) {
const gEle = element.closest('g');
for (let i = 0; i <= this.selectedRowFromMultiSeats.length; i++) {
if (this.selectedRowFromMultiSeats[i] !== gEle) {
this.selectedRowFromMultiSeats.push(gEle);
}
}
console.error(this.selectedRowFromMultiSeats);
答案 0 :(得分:1)
使用过滤器的标准方式,
let uniqueElements = selectedRowFromMultiSeats.filter((v, i, a) => a.indexOf(v) === i);
使用设置
let uniqueElements = [...new Set(selectedRowFromMultiSeats)];
答案 1 :(得分:0)
您可以使用Set,这将是非常方便的方法,并且将为您提供数组中的唯一元素 让uniqueElements = [... new Set(selectedRowFromMultiSeats)];