2017-03-16 75 views
0

我从列表中选择了多个ion-items,但我想从数组中删除多个相同的项目。如何在Angular 2中删除数组中的多个值

HTML代码

<div class="ion-item optionalItem"> 
    <button class="ion-button" ion-item *ngFor="let configop of interiorOption" (click)="itemSelected(configop)"> 
     <span class="color-name">{{ configop.intName }}</span> 
     <span class="color-price">{{ configop.price }}</span> 
    </button> 
</div> 

打字稿代码

this.Array.push(configop); 

for (let key in this.Array) { 
    this.Array2.push(this.Array[key]) 
} 

for(let i = 0; i < this.Array.length; i++) {  
    if(this.Array[i] === this.Array2[i]) 
    { 
     this.Array.pop(); 
    } 

    console.log("Sorted",this.Array); 
} 
+0

请更具体的问题是什么,你想得到什么样的结果 –

+0

我不想在我的数组中有多个相同的值。 如果用户按下“内部”按钮两次或多次。数组应该存储“内部”一次。不是多次。 我想弹出数组中的所有多个值。 @Bo Chen –

+0

为什么不只是检查数组是否存在,如果存在则抛出错误在用户说:“这个项目已被添加,请使用一个新的名称..”或类似的东西。 – DDelgro

回答

0

使用对象而不是数组将是一个更好的选择

点击按钮之前:

object = { 
    intName : false 
} 

点击按钮后:

if (!object.intName) { 
    object.intName = true; 
} 

输出的所有点击按钮:

for (let key in object) { 
    if(object.key) console.log(key); 
} 
0

这可以帮助清除dulpicates在数组中。

this.newArr = Array.from(new Set(this.oldArr.map((itemInArray) => itemInArray)));