私は次の問題で苦労しています:入れ子オブジェクトの検索と置換
私は入れ子オブジェクトを持っています。サーバーから、値が変更されたオブジェクトの応答が返されます。だから、私は入れ子になったオブジェクトの中のオブジェクトを見つけて置き換えたいと思う。 $は正しい方法を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内のオブジェクトではないネストされたループのためです。
誰でもこれを処理するアイデアはありますか?
[質問のタイトルにタグを入れないでください](https://stackoverflow.com/help/tagging)あなたは正しい – Liam
。私はそれが角度のための特定の問題だと思ったが、そうではない。 –