2017-04-10 7 views
-1

私は2つの配列を持っていますが、条件を満たす場合は、$ scope.checkDataで一致するオブジェクトを新しい配列にプッシュする必要があります。$ scope.checkDataから "val"を取得し、$ scope .postData条件が終了したら新しいアレイにプッシュインする方法は?

Javascriptを:事前に

$scope.postData= [ 
    { "pid": 1, "id": 1, "status": 1 }, 
    { "pid": 1, "id": 2, "status": 0 }, 
    { "pid": 1, "id": 3, "status": 1 }, 
    { "pid": 1, "id": 4, "status": 1 }, 
    { "pid": 1, "id": 5, "status": 0 }, 
    { "pid": 1, "id": 6, "status": 1 }, 
    { "pid": 1, "id": 7, "status": 1 } 
    ]; 

    $scope.checkData= [ 
     { "val": 1, "txt": "one" }, 
     { "val": 2, "txt": "two" }, 
     { "val": 3, "txt": "three" }, 
     { "val": 4, "txt": "four" }, 
     { "val": 5, "txt": "five" }, 
     { "val": 6, "txt": "six" }, 
     { "val": 7, "txt": "seven" 
    }]; 

    $scope.bindData = function (id) { 

      console.log(id); 
      $scope.someAry = []; 
      id.forEach(function (elem) { 
       $scope.postData.forEach(function (val) { 
       console.log(elem); 
       if (val.id== elem && val.status == true) { 
         alert("match found"); 
        } 
      else { 
         alert("match not found"); 
        } 

     }); 

感謝。

+0

希望の結果を追加してください。 –

+0

@ Nina Scholz期待した結果で質問を更新しました。一度確認してください。 – pbsbr

答えて

1
$scope.bindData = function (id) { 
    console.log(id); 
    $scope.someAry = []; 
    id.forEach(function (elem) { 
     $scope.postData.forEach(function (val) { 
      console.log(elem); 
      if (val.id == elem && val.status == 1) { 
       $scope.checkData.forEach(function (cval) { 
        if (cval.val == elem) { 
         $scope.newArray.push(cval); 
        } 
       }); 
      } 
     }); 
    }) 
} 
+0

こんにちは、 上記のコードは動作しています。 要素の配列を "$ scope.bindData"メソッドに渡すと、結果が "$ scope.newArray"に取得されます。 –

0

あなたはハッシュテーブルとハッシュテーブルとcheckDataをフィルタリングするための一つのループをSETINGための単一のループを使用することができます。

var $scope = {}, 
 
    hash = Object.create(null); 
 

 
$scope.postData = [{ pid: 1, id: 1, status: 1 }, { pid: 1, id: 2, status: 0 }, { pid: 1, id: 3, status: 1 }, { pid: 1, id: 4, status: 1 }, { pid: 1, id: 5, status: 0 }, { pid: 1, id: 6, status: 1 }, { pid: 1, id: 7, status: 1 }]; 
 
$scope.checkData = [{ val: 1, txt: "one" }, { val: 2, txt: "two" }, { val: 3, txt: "three" }, { val: 4, txt: "four" }, { val: 5, txt: "five" }, { val: 6, txt: "six" }, { val: 7, txt: "seven" }]; 
 

 
$scope.postData.forEach(function (a) { 
 
    hash[a.id] = a.status === 1; 
 
}); 
 
$scope.newArray = $scope.checkData.filter(function (a) { 
 
    return hash[a.val]; 
 
}); 
 

 
console.log($scope.newArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }

関連する問題