1

I was looking for solutions from Google and from this site. But I did not find the right answer for me. I have a json object:

$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 
 }
 }
 } 
 }
 }
}

This is a template menu, From this menu I need create new menu with angular 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);

How to write for delete "history" object from new menu?

Thanks, for all answers. Now, I change the question a bit. This is the $scope.jsonObject in the browser:

enter image description here

This is a menu creator:

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);

This is a code in 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>

This is a constructNewMenu() used instead of 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);
 }
 });
}

How to delete "no checked" menu, not knowing his level in the hierarchy of an object?

asked Aug 5, 2016 at 6:27
1
  • use delete of msdn. Commented Aug 5, 2016 at 6:33

4 Answers 4

0

You could either do

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

or just assign it to an empty json object

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

P.S. Your Json Object looks complicated. The above code has possibilities of can't get object of undefined errors. May be you could get away by doing null/undefined checks.

answered Aug 5, 2016 at 6:34
Sign up to request clarification or add additional context in comments.

Comments

0

if(key == "history"){

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

}

answered Aug 5, 2016 at 6:41

Comments

0

use delete of msdn

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

for more information read msdn delete

answered Aug 5, 2016 at 6:39

Comments

0

Use delete method :

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

for more information read msdn delete

Durgpal Singh
12k4 gold badges40 silver badges52 bronze badges
answered Aug 5, 2016 at 6:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.