【问题标题】:Remove Object from Json Object从 Json 对象中移除对象
【发布时间】:2016-12-11 11:20:45
【问题描述】:

我一直在寻找来自 Google 和这个网站的解决方案。但是我没有找到适合我的正确答案。我有一个 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);
        }
    });
}

如何删除“未选中”菜单,不知道他在对象层次结构中的级别?

【问题讨论】:

  • 使用删除msdn。

标签: angularjs json foreach


【解决方案1】:

使用delete方法:

if(key == "history"){
       delete  $scope.sortMenu.Card.next.operations.next.history;   
   }

更多信息请阅读msdn delete

【讨论】:

    【解决方案2】:

    使用删除msdn

    if(key == "history"){
       // I need delete object "history" from $scope.sortMenu    
       delete $scope.sortMenu.Card.next.operations.next.history;
    }
    

    更多信息请阅读msdn delete

    【讨论】:

      【解决方案3】:

      if(key == "历史"){

         delete  $scope.sortMenu.Card.next.operations.next[key];   
      

      }

      【讨论】:

        【解决方案4】:

        你可以这样做

        delete $scope.sortMenu.Card.next.operations.next.history;
        

        或者只是将它分配给一个空的json 对象

        $scope.sortMenu.Card.next.operations.next.history = {};
        

        附:您的 Json 对象看起来很复杂。上面的代码有可能出现can't get object of undefined 错误。也许您可以通过进行null/undefined 检查来逃脱。

        【讨论】:

          猜你喜欢
          • 2018-07-30
          • 2011-10-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-19
          • 2012-02-29
          • 2017-06-26
          相关资源
          最近更新 更多