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:
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?
-
use delete of msdn.Durgpal Singh– Durgpal Singh2016年08月05日 06:33:28 +00:00Commented Aug 5, 2016 at 6:33
4 Answers 4
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.
Comments
if(key == "history"){
delete $scope.sortMenu.Card.next.operations.next[key];
}
Comments
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
Comments
Use delete method :
if(key == "history"){
delete $scope.sortMenu.Card.next.operations.next.history;
}
for more information read msdn delete