2017-06-09 14 views
0

私は次の問題で苦労しています:入れ子オブジェクトの検索と置換

私は入れ子オブジェクトを持っています。サーバーから、値が変更されたオブジェクトの応答が返されます。だから、私は入れ子になったオブジェクトの中のオブジェクトを見つけて置き換えたいと思う。 $は正しい方法をscope.page内

var newItem = { 
    "type": "Post", 
    "label": "contact", 
    "id": "25" 
} 

どのようにオブジェクトを更新することができます。私は、新しいオブジェクトを取得し、サーバーから

$scope.page = { 
    id: 5, 
    label: 'myPage', 
    items : [ 
    { 
     "type": "Container", 
     "id": 1, 
     "label": "header", 
     "items": [ 
     { 
      "type": "Container", 
      "id": 2, 
      "label": "left", 
      "items": [ 
      { 
       "type": "Menu", 
       "label": "settings-menu", 
       "id": "5" 
      }, 
      { 
       "type": "Menu", 
       "label": "main-menu", 
       "id": "7" 
      } 
      ] 
     }, 
     { 
      "type": "Container", 
      "id": 4, 
      "label": "right", 
      "items": [ 
      { 
       "type": "Post", 
       "label": "contact", 
       "id": "25" 
      } 
      ] 
     } 
     ] 
    }, 
    { 
     "type": "Postlist", 
     "label": "nieuwsberichten", 
     "id": "17" 
    }, 
    { 
     "type": "HTML", 
     "label": "over deze site", 
     "id": "18" 
    }, 
    { 
     "type": "Other", 
     "label": "twitter feed", 
     "id": "19" 
    } 
    ] 
} 

私のオブジェクトは、このような構造を持っていますか?私は、次のことを試してみた:

$scope.findAndReplace(newItem,$scope.page.items); 

$scope.findAndReplace = function(newItem, items) { 
    for (var i = 0; i < items.length; i++) { 
    if (items[i].id == newItem.id) { 
     items[i] = newItem; 
    } else if (items[i].items) { 
     $scope.findAndReplace(newItem, items[i].items); 
    } 
    } 
} 

と:

var oldItem = $scope.findById(item.id, $scope.page.items); 
oldItem = newItem; 

$scope.findById = function(id, items) { 
    var match = null; 
    angular.forEach(items, function(i){ 
    if (match == null) { 
     if (i.id == id) { 
     match = i; 
     } else if (i.items) { 
     match = $scope.findById(id, i.items) 
     } 
    } 
    }) 
    return match; 
} 

これらのオプション作業のどちらを。これは、オブジェクトが$ scope.page内のオブジェクトではないネストされたループのためです。

誰でもこれを処理するアイデアはありますか?

+0

[質問のタイトルにタグを入れないでください](https://stackoverflow.com/help/tagging)あなたは正しい – Liam

+0

。私はそれが角度のための特定の問題だと思ったが、そうではない。 –

答えて

0

あなたの例はうまく見えますが、なぜ動作していないのか理解できません。

これらのオプションはどちらも機能しません。これは、オブジェクトが$ scope.page内のオブジェクトではないネストされたループのためです。

あなたは、こんにちは、私はあなたのためのフィドルを作成しているangular.copy(newItem, oldItem)

0

を使用して、オブジェクトの参照を保持することができます。 click for fiddle

for(var indx=0; indx < $scope.page.items.length; indx++) { 
    var tmpObj = $scope.page.items[indx]; 
    if(tmpObj.hasOwnProperty('items')) { 
    // check inside 
    for(var indx1=0; indx1<tmpObj.items.length; indx1++) { 
     var innerObj = tmpObj.items[indx1]; 
     // check for next level 
     if(innerObj.hasOwnProperty('items')) { 
     for(var counter=0; counter< innerObj.items.length; counter++) { 
      var thirdTmp = innerObj.items[counter]; 
      console.log('3rd level inner object', thirdTmp); 
      if(thirdTmp.id === newItem.id) { 
      innerObj.items[counter] = newItem; 
      tmpObj.items[indx1] = innerObj; 
      $scope.page.items[indx] = tmpObj; 
      } 
     } 
     } 

    } 
    } else if(tmpObj.id === newItem.id) { 
    $scope.page.items[indx] = newItem; 
    } 
}; 
+0

あなたの問題を解決したら、答えを受け入れてupvote ..幸運を忘れないでください:) –

+0

私は再帰的に検索したいです。あなたの例では、あなたのオブジェクトが深いほど頻繁に繰り返しループを書く必要があります。だから私のオブジェクトが10レベル深い場合は、私はfunctoimの内側に10ループを配置する必要があります –