2017-03-16 73 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)));