2015-12-12 62 views
0

我有一個小應用程序,用戶可以使用它來搜索電影,然後將其添加到其監視列表中。目前可以爲同一用戶添加1部電影。哪個課程不是期望的行爲。如何檢查值是否已經存在?

我的解決方案是找到正在添加的電影的唯一ID並用我的movies_users數據進行交叉檢查。如果movie_id值存在,請執行此操作,否則請執行此操作。

在我所具有的選擇要添加的電影的獨特影片ID的那一刻,

$scope.movieListID = response; 
console.log ($scope.movieListID.id) 

它得到ouputted像繩子,像這樣,

314365 

而且我得到了電影從目前的用戶記錄,

$scope.moviesCheck = response.data; 
console.log ($scope.moviesCheck) 

它看起來像這樣,

[{"id":2,"title":"Black Panther", "movie_id":"284054"}] 

那麼這將是檢查是否從$scope.movieListID.id結果在$scope.moviesCheck數據已經存在的好方法?

*更新*

嘗試建議以下不給預期的結果。

var exists = function (id) { 
    $scope.moviesCheck.forEach(function (movie) { 
    console.log (movie.movie_id) 
    console.log (id) 
    if (movie.movie_id === id) 
     console.log ('duplicate') 
    else 
     console.log ('not duplicate') 

    }); 
} 

exists($scope.movieListID.id); 

從這裏的console.log輸出,

312221 
312221 
not duplicate 

其中明確是重複的結果。

+0

不要使用三等份。直到值相同並且類型相同,結果纔會成立。例如,1 ==「1」將產生true。但1 ===「1」不會產生真實。檢查這個。 –

回答

1

您可以在控制器添加一個函數來檢查,如果在列表中存在

var exists = function (id) { 
    $scope.moviesCheck.forEach(function (movie) { 
     if (movie.id === id) 
      return true; 
    }); 

    return false; 
} 

// and call it 
exists($scope.movieListID.id); // true or false 
+0

謝謝你的建議。儘快嘗試一下,但問題很快。你認爲forEach會大幅影響瀏覽器的速度嗎?但我不期待很多電影的結果。 –

+0

沒有影響預計如果列表中有成千上萬的條目 – CaptainMat

+0

而不是foreach與本地的。 for循環一直比foreach的速度快40倍以上。 –

0

從你的問題我已經瞭解,基於對象使用ID對象的數組中存在的電影,你必須做不同的行動。

您可以使用$ filter來做到這一點。爲您的控制器注入過濾器並將其分配給示波器。所以,只要你想在這個控制器中就可以使用。

$scope.cFilter('filter')($scope.movies, {movie_id:$scope.existingMovie.movie_id}, true); 

$ sope.movi​​es - 是傳遞給過濾電影列表。你可以根據你的需要發送任何列表。

{movie_id:$ scope.existingMovie.movi​​e_id} - 這一個是對象 我們需要找到哪一個。這可以根據您的需要。由於我們 正在搜索movie_id,我們需要發送具有屬性 和值的對象。 {movie_id:$ scope.existingMovie.movi​​e_id},這裏movie_id是 的屬性,後面跟冒號的值。

true:這表示要搜索完全匹配的值。默認 這是錯誤的。如果這設置爲false,那麼如果我們想在電影ID中搜索54 ,則這將返回包含54 作爲值的一部分的對象。

app.controller( '控制器',[ '$過濾',函數($濾波器){$ scope.cFilter = $濾波器;

  $scope.Exists=function(){ 
$scope.movies=[{"id":2,"title":"Black Panther", "movie_id":"284054"},{"id":3,"title":"Black Panther", "movie_id":"32343"},{"id":4,"title":"Black Panther", "movie_id":"98863"}] 

$scope.existingMovie={"id":3,"title":"Black Panther", "movie_id":"32343"}; 

var _obj=$scope.cFilter('filter')($scope.movies, {movie_id:$scope.existingMovie.movie_id}, true); 
       if(_obj && _obj[0]) 
       { 
        Console.log('object exists') 
       } 
       else 
       { 
       Console.log('Object is not found') 
       } 
      } 

     }]) 
+0

恐怕我不明白這段代碼背後的邏輯。您使用過濾器和id作爲值,但我看不到您要過濾的內容。你有_obj作爲示例變量? –

+0

我用通用格式編寫。現在你可以看到,我修改了代碼。現在$ scope.movi​​es是電影的集合。 –

0

我不是100%,如果這爲做到這一點的好辦法,但對我來說它的工作原理,我認爲這是對性能相當低,

movieService.loadMovies().then(function(response) { 
    $scope.moviesCheck = response.data; 

    var arr = $scope.moviesCheck 
    function myIndexOf(o) { 
     for (var i = 0; i < arr.length; i++) { 
      if (arr[i].movie_id == o.exisitingMovie_id) { 
       return i; 
      } 
     } 
     return -1; 
    } 
    var checkDuplicate = (myIndexOf({exisitingMovie_id:movie.id})); 

    if (checkDuplicate == -1) { 
+0

這也看起來不錯。每個高級代碼都在內部實現。但是如果你要用同樣的方法一次又一次地寫,那麼你的代碼量就會增加。與其他相比沒有太大的不同。如果您想使用較少的代碼,請使用angular native中的$ filter。 –

0

非常感謝Jeeva JSB。這讓我在正確的軌道上,但是我想我會用澄清似乎按預期工作的實際例子。

所以我有一個名爲的getData這讓AJAX陣列功能,但我們需要檢查是否存在記錄之前加入到範圍(否則,我們得到一式兩份)

    if (d.data.length) { 
        for (var i = 0; i < d.data.length; i++) { 

         var doesExist = $filter('filter')($scope.notifications, {NotifId:d.data[i].NotifId}, true); 

         if (doesExist.length == 0){ 
          $scope.notifications.push(d.data[i]); 
         }       
        } 
       } 

這看起來......熟悉並且

當我們通過我們需要檢查的所述ID(在我的情況顯示通知)返回的AJAX對象迭代

var doesExist = $filter('filter')($scope.notifications, {NotifId:d.data[i].NotifId}, true); 

這行創建通過過濾存在一個新的數組在作用域內的數組($ scope.notifications)並從你的interation中傳遞相同的值。

如果該值存在,則該對象將被複制到名爲doesExist的新數組中。

一個簡單的長度檢查將決定是否需要寫入記錄。

我希望這可以幫助別人。