2017-04-13 8 views
1

arrayリストから重複オブジェクトを削除し、代わりにアイテム数量プロパティを更新する方法を見つけようとしています。私はオブジェクトを私のsharedServiceにプッシュしています。配列内の重複オブジェクトを避け、角度2のアイテム数量を更新する方法

item component - これは数量を1だけ更新しますが、配列内の同じオブジェクトをプッシュします。 if()addToBasket()またはSharedServiceにあるはずですが、確かにif()のステートメントが間違っていることを確認してください。

一意のオブジェクトを配列にプッシュし、同じオブジェクト/アイテムを追加するときに数量を更新するにはどうすればよいですか?値2のようになります

addToBasket() { 
let items = { 
    id: '123', 
    quantity: 1 
}; 

if (items) { 
    items.quantity = parseInt(items.quantity) + 1; 
} else { 
    this.basketArr = { 
    id: items.name, 
    quantity: items.quantity 
    } 
} 
this.sharedService.itemCollection(items); 
} 

マイSharedServiceこれまでのところだけ保持しているarrayオブジェクト

itemCollection(id) { 
    this.itemArr.push(id); 
    console.log(this.itemArr); 
} 

所望の結果が更新された量です。

[{ id: '123, quantity: 2 }] 

の代わりに - 新しいアイテムが配列にプッシュする前に存在しているかどうかを

[ 
{ id: '123, quantity: 2 }, 
{ id: '123, quantity: 2 } 
] 

Plnkr Sample

答えて

2

チェック。

itemCollection(id) { 
    // check whether id exists 
    var index = this.itemArr.findIndex(item => item.id === id.id); 

    if (index > -1) { 
    // check quantity to determin whether replace the exist one or not 
    if (id.quantity === this.itemArr[index].quantity) { 
     return; 
    } else { 
     // update quantity for the exist one 
     this.itemArr[index].quantity = id.quantity; 
    } 
    } else { 
    // add the new item which dosen't exist 
    this.itemArr.push(id); 
    } 

    console.log(this.itemArr); 
} 

サービス内のすべてのもの

itemCollection(id) { 
    // check id only 
    var index = this.itemArr.findIndex(item => item.id === id.id); 

    if (index > -1) { 
    this.itemArr[index].quantity = this.itemArr[index].quantity + 1; 
    } else { 
    this.itemArr.push(id); 
    } 

    console.log(this.itemArr); 
} 
+0

どのように量を更新しましたか? – MrNew

+0

@MrNew 'addToBasket'では' quantity'は常に2になるので、 'id'のチェックだけを提案します。 'id'と' quantity'の両方を 'Array.findIndex'でチェックするか、単にサービスで行われる' quantity'のインクリメントを動かすだけです。 – Pengyy

+0

どういうわけか、新しい/別のオブジェクトを配列に追加せず、配列内のオブジェクトの量を更新するだけです。上記のコードを編集することもできますが、コードブロックが壊れているようです。 – MrNew

0

はこれを試してください:

itemCollection(item) { 
    let itemSearch = this.itemArr.filter(function(value){ 
    if(item.id == value.id) 
     return value; 
    }) 

    if(itemSearch.length == 0) 
    { 
    this.itemArr.push(item); 
    } 
    else 
    { 
    let index = this.itemArr.indexOf(itemSearch[0]); 
    this.itemArr[index].quantity = item.quantity; 
    } 
    console.log(this.itemArr); } 
1

これを試してみてください。 https://plnkr.co/edit/2oR6vA5hJcRbzR0dtNE5?p=preview

サービスパーツをこれに変更しました。

import { Injectable } from '@angular/core'; 

@Injectable() 
export class SharedServiceService { 
    public itemArr: any = []; 
    constructor() { } 

    itemCollection(id) { 
    var obj=this.checkUniqueCondition(id); 
    console.log(this.itemArr); 
    } 

    checkUniqueCondition(id){ 
    if(this.itemArr.length>0){ 
    for(var a in this.itemArr){ 
     if(this.itemArr[a].id==id.id){ 
     this.itemArr[a].quantity=this.itemArr[a].quantity+id.quantity; 
     return this.itemArr[a]; 
     }else{ 
     console.log("in else"); 
     this.itemArr.push(id); 
     } 
    } 
    }else{ 
    this.itemArr.push(id); 
    } 

} 

一致するものが見つかると、既存のオブジェクトに追加されます。

this.itemArr[a].quantity=this.itemArr[a].quantity+id.quantity; 
+0

ありがとうございます:)それを受け入れるが、@ Pengyyの2番目のコードブロックasnwerを使用します。 – MrNew

+0

あなたはそれを+1することができます:p –

+0

ああ、私はやったと思って、もう一度やります:) – MrNew

関連する問題