0

I am working on one small project. I stuck with one small js issue. i have json string like:

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"
                    }  
                ]
            }
        }
    };

I want to filter it like:

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

so far i have tried with below code but i din get the success.

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

Please help me thanks in advance.

2
  • 1
    parse not the right word for this function. Parsing usually means you're applying some kind of state machine to transform a plain text into a structure. transform would be a better term. Commented Sep 25, 2013 at 12:15
  • @Frits van Campen ok i will change it thanks. do you have any solution for my issue? Commented Sep 25, 2013 at 12:21

1 Answer 1

1

Well, a simple algorithm (supposing that the key names ID and children will not change)

  1. Create an empty object N
  2. Iterate on the keys and values of O
    • If the value is an object, let O be this object and go to 2.
    • If the key is children, for each child let O be the child and go to 2.
    • If the key is ID
      1. Let name be the value for this key
      2. Create an empty object X.
      3. Copy all the key and values of O to X except ID and children.
      4. Assign the built object to the key name in your object N

Here is a simple implementation (not flexible at all though) of the above algorithm.

var createdFormattedObject = function(baseObject, formattedObject) {
  formattedObject = formattedObject || {};

  for(var key in baseObject) {
    if(!baseObject.hasOwnProperty(key)) {
      continue;
    }
    var value = baseObject[key];

    if(key === "ID") {
      formattedObject[value] = {};
      for(var k in baseObject) {
        if(k !== "ID" && k !== "children") {
          formattedObject[value][k] = baseObject[k];
        }
      }
    } else if(key === "children") {
      for(var i = 0; i < value.length; i++) {
        createdFormattedObject(value[i], formattedObject);
      }
    } else if(value instanceof Object) {
      createdFormattedObject(value, formattedObject);
    }
  }

  return formattedObject;
};

Note that the second parameter is only useful for recursing and is not necessary when calling the function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.