2016-07-13 7 views
0

したがって、最近プッシュされた配列オブジェクトの存在を確認しようとしています。 私はangularJSを使ってWebアプリケーションを開発しています。配列オブジェクトの存在を確認する

私は私モジュールをCRUDをユーザに聞かせてフォームを有する

vm.data.detail 

として定義される配列を有します。モジュールは、製品の在庫量についてです。私の角度コントローラでaddProduct()関数を実行するボタンがあります。

addProduct機能:配列はただ一つの製品で構成されている場合

function addProduct() { 

    //to check whether the array is empty. 
    if (vm.data.detail.length == 0) { 
    vm.data.detail.push({ 
     product: vm.data.product_id.selected, 
     current_qty: vm.data.product_id.selected.qty, 
     new_qty: Number(vm.data.product_id.selected.qty) - Number(1), 
     difference: Number(vm.data.product_id.selected.qty) - (Number(vm.data.product_id.selected.qty) - Number(1)), 
     remarks: '' 
    }); 
    console.log("Product just been added"); 
    } 

    //if the array is not empty 
    else { 
    for (var i = 0; i < vm.data.detail.length; i++) { 

     //to check whether the selected product is already inside the array 
     if (vm.data.product_id.selected.name == vm.data.detail[i].product.name) { 
     console.log("same product selected"); 
     //data 
     } 

     //if there is no selected product inside the array, then add it 
     else { 
     console.log("different product has just been selected"); 
     vm.data.detail.push({ 
      product: vm.data.product_id.selected, 
      current_qty: vm.data.product_id.selected.qty, 
      new_qty: 0, 
      difference: 0, 
      remarks: '' 
     }); 
     } 
    } 
    } 
} 

上記のコードはうまく動作します。この問題は、別の製品Bを製品に追加しようとしているときに発生します。条件は次のとおりです。

  1. 製品Aは既にアレイ内にあります。
  2. 製品Bが選択され、アレイに追加されます。現在、アレイは2つの製品で構成されています。
  3. 新しい製品Bを追加するテストを行っているときに、まだ新しい製品Bで配列がプッシュされている理由がわかりません。したがって、配列は3つの製品(1製品Aと2製品B) 。

私は二、製品Bを追加しようとしていたときで、配列は私がここで行方不明です何新製品B.

で押されることはありません望んでいましたか?数時間それを処理していると私は "検証"のために追加する必要があります把握することはできません。

アレイプッシュするオブジェクトが既に正しいことに注意してください。私はちょうどif elseを置く方法を知らない。内部のロジックがまだ不足しているように見えますが、何が欠けているのか分かりませんでした。

ありがとうございました。

+0

私はPHPを見ないので、なぜタグですか? – RiggsFolly

+0

私の悪い先生のために申し訳ありません、私は間違って考えました:) – Vinfoster0701

+0

ははるかに複雑に見える必要があります。これがどのように使われているかを見るためにはビューを見る必要があります – charlietfl

答えて

0

する必要があり、あなたは外に移動する必要があります。

ヒント:

あなたは要素がforループ伝統的なを行うよりもはるかに優れている配列に存在するかどうかを確認するためにArray.prototype.find()メソッドを使用することができます。

あなたはドキュメントに気づいたかもしれませんが、あなたがこの互換性が必要な場合、あなたはArray.prototype.filter()を使用することができますので、方法を見つけるには、IEとOperaブラウザとは互換性がありません。私はそれが役に立てば幸い

function addProduct() { 
    //to check whether the array is empty. 
    if (!vm.data.detail.length) { // you can simply use (!length) instead of comparing with 0 
    vm.data.detail.push({ 
     product: vm.data.product_id.selected, 
     current_qty: vm.data.product_id.selected.qty, 
     new_qty: Number(vm.data.product_id.selected.qty) - Number(1), 
     difference: Number(vm.data.product_id.selected.qty) - (Number(vm.data.product_id.selected.qty) - Number(1)), 
     remarks: '' 
    }); 
    console.log("Product just been added"); 
    } 

    //if the array is not empty 
    else { 
    // if there's no obj with the same name inside the array, it returns undefined, otherwise it returns the object. 
    var obj = vm.data.detail.find(function(value) { 
     return value.product.name == vm.data.product_id.selected.name; 
    }); 

    /* Using FILTER: 
    var obj = vm.data.detail.filter(function(value) { 
     return value.product.name == vm.data.product_id.selected.name; 
    })[0]; 
    */ 

    // Now you can test, if the object exists 
    if (obj) { 
     console.log("same product selected"); 
    } 
    //if there is no selected product inside the array, then add it 
    else { 
     console.log("different product has just been selected"); 
     vm.data.detail.push({ 
     product: vm.data.product_id.selected, 
     current_qty: vm.data.product_id.selected.qty, 
     new_qty: 0, 
     difference: 0, 
     remarks: '' 
     }); 
    } 
    } 
} 

:両方のバージョン(フィルタを見つける。)とし、また、必要な修正を加えて以下

は、コードです!

+1

Sir。あなたはロック!ありがとう! – Vinfoster0701

0

これは基本的な論理エラーです。あなたは、あなたがする必要がどのような

for each element { 
    if element is different from given { 
     add given to array 
    } 
} 

をやっている

var allElementsDifferentFromGiven = true 
for each element { 
    if element is same as given { 
     allElementsDifferentFromGiven = false 
     break 
    } 
} 
if (allElementsDifferentFromGiven) { 
    add given to array 
} 

である。しかし、JavaScriptの配列は、それを行うためのメソッドを持っている:

if (array.every(function(element) { 
    return true if element is different given 
})) { 
    add given to array 
} 
-1

私は、問題はここにあると思う: が製品ため、詳細オブジェクト上のプロパティであり、製品は名前プロパティを持たず、条件に合致せず、条件を満たして名前リストにプッシュします。

//to check whether the selected product is already inside the array 
    if(vm.data.product_id.selected.name == vm.data.detail[i].product.name){ 

それはあなたが、アレイ内elseのチェックを行っている、単純なミスをコミットしている

//to check whether the selected product is already inside the array 
    if(vm.data.product_id.selected.name == vm.data.detail[i].product){ 
     console.log("same product selected"); 
     //data 
    } 
関連する問題