0

I am looking to create hashmap like array which will contain key and value derived from another array which has nested objects.

so i am trying the following code.

    var testhash={},data=[
      {
        "yang_type": "container",
        "name": "c1",
        "value": "",
        "children": [
          {
            "yang_type": "container",
            "name": "c2",
            "value": "",
            "children": [
              {
                "yang_type": "list",
                "name": "Car",
                "value": "",
                "children": [
                  {
                    "yang_type": "leaf",
                    "name": "wheels",
                    "value": "",
                    "children": [
                      {
                        "name": "max-elements",
                        "value": "4",
                        "children": [],
                        "yang_type": ""
                      }
                    ]
                  }
                ]
              },
              {
                "yang_type": "",
                "name": "text",
                "value": "4",
                "children": []
              }
            ]
          }
        ]
      }
    ];

 var k='';
                    function loop1(a, depth) {
                        var l,s='';
                        if(depth){s += '/';}
                        k=k+a.yang_type+a.name+a.value;
                        v=Array(depth + 1).join(s) + a.yang_type+a.name+a.value;
                        testhash.push(k:v);
                        //console.log(l);
                          //console.log(Array(depth + 1).join("/") + a.yang_type,a.name,a.value);
                          //hashServiceParams.push(Array(depth + 1).join("/") + a.yang_type,a.name,a.value);

                          Array.isArray(a.children) && a.children.forEach(function(child) {
                              loop1(child, depth + 1);
                          });
                        }
                    console.log(testhash);

The output, I am expecting is

{"containerc1":*,"containerc2":"containerc1/containerc2","listcar":"containerc1/containerc2/listcar","leafwheels":"containerc1/containerc2/listcar/leafwheels","max-elements":"containerc1/containerc2/listcar/leafwheels/max-elements","text4":"containerc1/text4"}

The above array will act as an hash map that contains key and value , where value stores the part of that data in the tree structure.

my code just calculates the depth and adds / to each level it moves down but i expect the output to be as shown above. Any recommendation coders ?

1 Answer 1

1

The following should do the trick (use either version according to your needs):

ECMAScript 6:

function parseData(data, prefix) {
  let result = {};
  data.forEach(o => {
    const key = `${o.yang_type}${o.name}`;
    result[key] = prefix ? `${prefix}/${key}` : '*';
    if (o.children) {
      const newPrefix = prefix ? `${prefix}/${key}` : key;
      result = Object.assign(result, parseData(o.children, newPrefix));
    }
  });
  return result;
}

ECMAScript 5:

function shallowMerge(obj1,obj2){
  var obj3 = {};
  for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
  for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
  return obj3;
}

function parseData(data, prefix) {
  var result = {};
  data.forEach(function (o) {
    var key = '' + o.yang_type + o.name;
    result[key] = prefix ? prefix + '/' + key : '*';
    if (o.children) {
      var newPrefix = prefix ? prefix + '/' + key : key;
      result = shallowMerge(result, parseData(o.children, newPrefix));
    }
  });
  return result;
}

In order to use it you simply need to do the following:

let testhash = parseData(data);

This will populate the testHash with the result you need.

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

11 Comments

should this work as java script or should i include any specific libraries ?. Sorry this is the first time . I am hearing abt ecma script. Is there a way to have this convereted to JS ?
ECMAScript is the "version" of javascript. The above snippet is written in version 6 which is not compatible with all browsers. I will update my answer with a version 5 one that will work without a problem.
Thank you for the answer .. Let me check it and Thank more :)
Thanks, This works like a charm but however if i would need to replace the output to {"0":*,"1":"containerc1/containerc2","2":"containerc1/containerc2/listcar","3":"containerc1/containerc2/listcar/leafwheels","4":"containerc1/containerc2/listcar/leafwheels/max-elements","5":"containerc1/text4"}
Pass an optional number as argument to the function that will be incremented in each step. Then use it in the place of the key.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.