我一直在寻找谷歌和本网站的解决方案。但我找不到合适的答案。我有一个json对象:
$scope.jsonObject = {
"Card":{
"type":"menu",
"options":["option1","option2"],
"name":"With card",
"next":{
"operations":{
"type":"menu",
"options":["option1","option2"],
"name":"Card Operations",
"next":{
"balance":{
"type":"transaction",
"options":["option1","option2"],
"name":"Get Balance",
"next":null
},
"history":{
"type":"transaction",
"options":["option1","option2"],
"name":"History Card",
"next":null
}
}
}
}
}
}
这是一个模板菜单,从这个菜单我需要创建带有角度foreach的新菜单:
$scope.sortMenu = {};
function sortObject(menu){
angular.foreach(menu, function(key,value){
if(key == "card"){
$scope.sortMenu = menu;
}
if(key == "history"){
// I need delete object "history" from $scope.sortMenu
}
sortObject(value.next);
});
}
sortObject($scope.jsonObject);
谢谢你的回答。现在,我稍微改变了一下这个问题。这是浏览器中的$ scope.jsonObject:
这是一个菜单创建者:
function showDefaultMenu(menu,iterator){
angular.forEach(menu, function(value, key){
console.log(key);
$('#myTree').append(
"<div class='col-md-12 col-xs-12'>" +
"<input type='checkbox' id='"+key+"' value='"+key+"' style='position: inherit;margin-left:"+iterator+"px'>"+key+
"</div>"
);
if(value.next !== undefined){
showDefaultMenu(value.next,iterator+20);
}
});
}
showDefaultMenu($scope.jsonObject,20);
这是html中的代码:
<div id="myTree" class="col-md-12 col-xs-12" style="margin-top: 50px;border:1px solid black;">
</div>
<div class="col-md-12 col-xs-12" style="margin-top: 10px;" >
<input type="button" ng-click="constructNewMenu()" class="button btn-xs btn-info" value="Create Menu" />
</div>
这是constructNewMenu()
而不是sortObject()
:
function constructNewMenu(){
assistantConstructMenu($scope.jsonObject);
}
function assistantConstructMenu(menu){
angular.forEach(menu, function(value, key){
if ($('#' + key).is(':checked')) {
if (key === "card") {
$scope.sortMenu.push(menu);
}
}else{
// I need delete object "no checked" from $scope.sortMenu
}
if(value.next !== undefined){
assistantConstructMenu(value.next);
}
});
}
如何删除“未选中”菜单,不知道他在对象层次结构中的级别?
答案 0 :(得分:0)
你可以做
delete $scope.sortMenu.Card.next.operations.next.history;
或只是将其分配给空的json
对象
$scope.sortMenu.Card.next.operations.next.history = {};
P.S。你的Json对象看起来很复杂。上述代码可能会出现can't get object of undefined
错误。也许你可以通过null/undefined
检查来逃避。
答案 1 :(得分:0)
使用if(key == "history"){
delete $scope.sortMenu.Card.next.operations.next.history;
}
方法:
@property (nonatomic, retain) NSMutableArray *array1;
了解更多信息,请阅读msdn delete
答案 2 :(得分:0)
使用删除msdn
if(key == "history"){
// I need delete object "history" from $scope.sortMenu
delete $scope.sortMenu.Card.next.operations.next.history;
}
了解更多信息,请阅读msdn delete
答案 3 :(得分:0)
if(key ==&#34; history&#34;){
delete $scope.sortMenu.Card.next.operations.next[key];
}