2017-08-25 75 views
1

我有以下JSON(樣本數據)i。從另一個URL比較對象數組中angularjs

var data = [ 
{ id: 1, name: 'ravi', parentid: 0 }, 
{ id: 2, name: 'raj', parentid: 1 }, 
{ id: 3, name: 'ram', parentid: 1 }, 
{ id: 4, name: 'raja', parentid: 0 }, 
{ id: 5, name: 'raju', parentid: 4 },        
{ id: 6, name: 'dinesh', parentid: 4 }   
]; 

當我在角成功消息,我想比較數據獲得,其具有的ID parentid零剩餘data.I的parentid嘗試下面的代碼,但我不能夠進行比這

$http.get('URL') 
    .success(function(data) { 
     var categories = data; 
     for(var i =0;i<=categories.length;i++) 
     { 
      if(typeof categories[i] != "undefined" && categories[i].parentId == 0) 
      { 
       $scope.cat.push(categories[i]); 
      } 
      if($scope.cat[i].parentid == categories[i].id) 
      { 
       $scope.content.push(categories[i]); 
      }   
     } 

    }); 

在這裏,我想比較categories[i].parentId$scope.cat.id越來越插入ARR唉,但在比較這我得到這樣的錯誤$scope.cat[i]undefined 終於我的輸出看起來像下面

Ravi 
    raj 
    ram 
raja 
    raju 
    dinesh 
+0

'我<= categories.length'應該是'我在你的for循環 – user2718281

+0

還是我得到錯誤 user7098427

+0

我沒有在您的代碼中找到'$ scope.cat'定義的任何地方。它應該是什麼? – user2718281

回答

1

我面臨如下三個問題:

  1. if(typeof categories[i] != "undefined" && categories[i].parentId == 0)你永遠比較parentId它是因爲你的房產名稱是parentid(檢查套管)。因此,你的第一個if循環永遠不會執行,這就是爲什麼你的$scope.cat保持未定義。

解決方案:修正錯字爲parentId

  • 在,if($scope.cat[i].parentid == categories[i].id)您正在比較子元素的ID的父元素的父ID。
  • 解決方案:if($scope.cat[i].id == categories[i].parentid)

  • 您正在使用相同的迭代器用於與兩種不同長度兩個列表項。
  • 解決方案:

    var categories = data; 
    
    // fill $scope.cat with elements having parent id = 0, so this list will contain all the parent elements 
    $scope.cat = categories.filter(function(category) { 
        return category && category.parentid === 0 
    }); 
    $scope.content = []; 
    // fill $scope.content with elements whose parent exist in $scope.cat (the parent list craeted above) 
    categories.forEach(function(category) { 
        if ($scope.cat.filter(function(cat) { 
          return category.parentid === cat.id 
         }).length > 0) 
         $scope.content.push(category); 
    }); 
    

    樣品plunk here

    +0

    即使我改變類別[i] .parentid我得到相同的錯誤 – user7098427

    +0

    @ user7098427:我更新了我的答案與優化的代碼,看看 –

    +0

    我越來越undefined在我的控制檯,如果我使用你的code.could你請讓我看到plnkr – user7098427

    0

    嘗試是這樣的

    更換

    if($scope.cat[i].parentid == categories[i].id) 
          { 
           $scope.content.push(categories[i]); 
          } 
    

    var res = $scope.cat.filter(function(item){ 
        return item.parentid === categories[i].id; 
    }); 
    
    if(res.length) 
    { 
        $scope.content.push(categories[i]); 
    } 
    
    +0

    你能告訴我在PLUNK嗎? – user7098427

    +0

    嘗試我建議添加什麼你應該替換,我唯一擔心的是你可能需要使用過濾器來匹配的內容和貓的ID與IDS。 – Jaie

    0

    你爲$scope.cat[i]得到undefined因爲推向$scope.cat代碼並不總是甚至對於數據陣列的一些值這個條件不會是真的修正parentIdparentid

    if(typeof categories[i] != "undefined" && categories[i].parentid == 0) 
    { 
           $scope.cat.push(categories[i]); 
    } 
    

    後執行。所以沒有什麼會推到$scope.cat

    並記住您的第二個if聲明緊隨您的第一個if聲明之後。所以有時候你會不會有$scope.cat[i]

    所有這些數值的代碼不會被執行怎麼一回事,因爲他們不通過的情況

    { id: 2, name: 'raj', parentid: 1 }, 
    { id: 3, name: 'ram', parentid: 1 }, 
    { id: 5, name: 'raju', parentid: 4 },  
    { id: 6, name: 'dinesh', parentid: 4 } 
    

    因爲parent id0

    因此,這些都是造成undefined

    可以改爲:

    var categories = data; 
        for(var i =0;i<=categories.length;i++) 
        { 
         if(typeof categories[i] != "undefined" && categories[i].parentid ==0) 
         { 
          $scope.cat.push(categories[i]); 
         } 
    
        } 
        //you will already have you $scope.cat filled then loop through it 
        //checking with the categories array too. 
    
        for(var j=0;j<$scope.cat.length;j++) 
        { 
         for(var k=0; k<categories.length; k++){ 
         if($scope.cat[j].parentid==categories[k].id) 
         { 
          $scope.content.push(categories[k]); 
         } 
         } 
        } 
    

    或類似的東西:

    var categories = data; 
    
    categories = categories 
    .filter((category)=>{ 
        return (category !== []._); //removes all undefineds in the array 
    }) 
    .map((category)=>{ 
        //checks each category from data if is parent_Id is 0 and pushes it to cats array 
        //by the end of the operation. the cats array will have some data 
        (category.parentId == 0)? ($scope.cats.push(category) , return) : return; 
    }) 
    var getContent =(cats,categories)=>{ 
        var content=[]; 
        for(var j=0;j<cats.length;j++) 
        { 
        for(var k=0; k<categories.length; k++){ 
         if(cats[j].parentid==categories[k].id) 
         { 
         content.push(categories[k]); 
         } 
        } 
        } 
        return content; 
    } 
    $scope.content = getContent($scope.cats,categories); 
    
    +0

    在這種情況下,我有$ scope.cat.so中的兩個數據我想只比較這兩個數據的id與其餘數據的殘差 – user7098427

    +0

    記住你的第二個if語句是在你的第一個if stattement之後。所以有時你不會有Scope.cat [我] – henrybbosa

    +0

    我要編輯的答案 – henrybbosa