Currently, I am tackling a small project and facing a minor JS issue. The JSON string that I have looks like this:
var jsObj = {
"templates": {
"form0": {
"ID": "MyAlertNew",
"isVisible": "true",
"children": [
{
"-type": "kButton3",
"ID": "myButtonID1",
"isVisible": "true",
"onClick": "onClickMethod",
"paddings": {
"left": "0",
"right": "0",
"top": "3",
"bottom": "3",
"unit": "%"
},
"text": "dynamic text in file"
},
{
"-type": "kButton1",
"ID": "btnAlign2",
"visible": "true",
"margins": {
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"unit": "%"
}
}
]
},
"form1": {
"ID": "frmNewPOC1",
"isVisible": "true",
"children": [
{
"-type": "kButton3",
"ID": "btnAlign",
"isVisible": "true",
"onClick": "onClickMethod",
"paddings": {
"left": "0",
"right": "0",
"top": "3",
"bottom": "3",
"unit": "%"
},
"text": "in diff form"
},
{
"-type": "kButton1",
"ID": "btnAlignTest",
"visible": "true",
"margins": {
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"unit": "%"
},
"text": "in My Form"
}
]
}
}
};
In order to filter it as shown below:
objnew ={
"MyAlertNew":{
"isVisible": "true"
},
"myButtonID1":{
"isVisible": "true",
"onClick": "onClickMethod",
"paddings": {
"left": "0",
"right": "0",
"top": "3",
"bottom": "3",
"unit": "%"
},
"text": "dynamic text in file"
},
"btnAlign2":{
"visible": "true",
"margins": {
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"unit": "%"
}
},
"frmNewPOC1":{
"isVisible": "true"
},
"btnAlign":{
"isVisible": "true",
"onClick": "onClickMethod",
"paddings": {
"left": "0",
"right": "0",
"top": "3",
"bottom": "3",
"unit": "%"
},
"text": "in diff form"
},
"btnAlignTest":{
"visible": "true",
"margins": {
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"unit": "%"
},
"text": "in My Form"
}
}
I have attempted the code below but haven't achieved success yet
testObj = jsObj.templates;
var idObj = [];
var widgetProp =[];
var ID ;
function parseMyObj(testObj){
for(x in testObj){
if(typeof testObj[x] == "object"){
//widgetProp[]
parseMyObj(testObj[x]);
}
else if(x=='ID'){
ID = testObj[x];
idObj.push(testObj[x]);
}
else{
widgetProp.push(x:testObj[x]);
}
}
}
parseMyObj(testObj);
console.log(widgetProp);
console.log(JSON.stringify(idObj));
If anyone could provide assistance, it would be greatly appreciated. Thank you in advance.