I am dealing with the following JSON data. My goal is to remove only the children nodes from the array while keeping the rest of the content intact.
{
"model_type_name": "portfolio",
"hier_model_type_name": "portfolio",
"object_type": "product",
"gen_new_flag": "n",
"mkt_map_req_flag": "n",
"rule_based_flag": null,
"custom_price_type": null,
"hier_id": 468299069,
"hier_name": "AMS",
"parent_hier_id": 1,
"parent_hier_name": "Portfolio",
"level": "2",
"isLeaf": "0",
"parentage": "\\Portfolio\\AMS",
"hier_gen_new_flag": "y",
"hier_child_gen_new_flag": "y",
"hier_entity_role": "parent",
"hier_child_entity": "portfolio",
"hier_rel_type_name": "portfolio hierarchy",
"hist_flag": "y",
"hier_hist_flag": "y",
"rls_type": null,
"mass_updt_flag": null,
"children": [
{
"model_type_name": "dummy",
"hier_model_type_name": "portfolio",
"object_type": "product",
"gen_new_flag": "n",
"mkt_map_req_flag": "n",
"rule_based_flag": null,
"custom_price_type": null,
"hier_id": 469444670,
"hier_name": "Integrated Solutions",
"parent_hier_id": 468299069,
"parent_hier_name": "AMS",
"level": "3",
"isLeaf": "0",
"parentage": "\\Portfolio\\AMS\\Integrated Solutions",
"hier_gen_new_flag": "y",
"hier_child_gen_new_flag": "y",
"hier_entity_role": "parent",
"hier_child_entity": "portfolio",
"hier_rel_type_name": "portfolio hierarchy",
"hist_flag": "y",
"hier_hist_flag": "y",
"rls_type": null,
"mass_updt_flag": null,
"children": [
{
"model_type_name": "dummy",
"hier_model_type_name": "portfolio",
"object_type": "product",
"gen_new_flag": "n",
"mkt_map_req_flag": "n",
"rule_based_flag": null,
"custom_price_type": null,
"hier_id": 469444678,
"hier_name": "Healthcare",
"parent_hier_id": 469444670,
"parent_hier_name": "Integrated Solutions",
"level": "4",
"isLeaf": "1",
"parentage": "\\Portfolio\\AMS\\Integrated Solutions\\Healthcare",
"hier_gen_new_flag": "y",
"hier_child_gen_new_flag": "n",
"hier_entity_role": "parent",
"hier_child_entity": "portfolio",
"hier_rel_type_name": "portfolio hierarchy",
"hist_flag": "y",
"hier_hist_flag": "y",
"rls_type": null,
"mass_updt_flag": null,
"children": null,
"hierGenNewFlag": true,
"releaseDriven": false,
"genNewFlag": false,
"hierChildGenNewFlag": false,
"massUpdateFlag": false
}
],
"hierGenNewFlag": true,
"releaseDriven": false,
"genNewFlag": false,
"hierChildGenNewFlag": true,
"massUpdateFlag": false
}
],
"hierGenNewFlag": true,
"releaseDriven": false,
"genNewFlag": false,
"hierChildGenNewFlag": true,
"massUpdateFlag": false
}
Here's what I've tried so far, but unfortunately, it's not working as expected. Any help would be greatly appreciated.
for(var i=0; i< $scope.selectedNode.length; i++){
var obj = scope.selectedNode[i];
for(var k in obj){
if(k == "children"){
if(!obj[k]){
delete $scope.selectedNode.children;
}
}
}
}