Nadvez Nadvez - 5 months ago 14
JSON Question

Javascript to manipulate array of objects and create two set of arrays for data and link

I have an object array like the following.

[
{
"name": "car",
"value": "",
"children": [
{
"name": "v8_engine",
"value": "",
"children": [
{
"name": "cylinder-arrangement",
"value": "",
"children": [
{
"name": "type",
"value": "string",
"children": []
},
{
"name": "max-elements",
"value": "8",
"children": []
}
]
}
]
},
{
"name": "other-parts",
"value": "",
"children": [
{
"name": "per-cylinder-parts",
"value": "",
"children": [
{
"name": "piston-diameter",
"value": "",
"children": [
{
"name": "type",
"value": "uint32",
"children": []

},
{
"name": "range",
"value": "2000... 9000",
"children": []

}
]
},
{
"name": "valves",
"value": "",
"children": [
{
"name": "number",
"value": "",
"children": []
},
{
"name": "position",
"value": "",
"children": []
}
]
}
]
}
]
}
]
}
]


I want to parse through each elements and their respective childrens and manipulate it to create two set of arrays


  1. Node data array that contains:key which is index of that element and values as shown



nodeDataArray.push({ key:i,Data: a.yang_type + " " + a.name}) or nodeDataArray.push({ key:i,Data: a.name + " " + a.value})


  1. Link Data array that contains the link (parent child relation ship)

    linkDataArray.push({ from: i, to: j });



where i is the index of parent and j is index of child

I have the following function that parses through the elements and pushes them fine in to node data array with index.

vm.tree.forEach(loop);// here vm.tree is the json data, passed dynamically
var i=0;
function loop(a) {

if(a.yang_type!='' && a.name!=''){
nodeDataArray.push({ key:i,Data: a.yang_type + " " + a.name, group: -1 });
//console.log("Data:",a.yang_type);
linkDataArray.push({ from: i, to: i+1 });
}

if(a.name!='' && a.value!=''){
nodeDataArray.push({ key:i,Data: a.name + " " + a.value, group: -1 });
linkDataArray.push({ from: 0, to: i+1 });

}

i=i+1;
// process you data
//if(Array.isArray(a.children)){j++;}
if(Array.isArray(a.children)){

//var g=0;
a.children.forEach(loop);
}


}


Below wordings is based on the sample JSON to make it more clear on what is my expected output should be


  1. parse through the JSON and list out all the elements in the JSON object as shown below

    car
    v8_engine
    cylinder-arrangement
    type string
    max-elements 8
    other_parts
    per-cylinder-parts
    piston-diameter
    type UINT32
    range 2000...3000
    valves
    number
    position

  2. Then list of relationship based on parent and child index. Where car is the 0th element,v8_engine is the 2nd and so on … until the last one which is position being 12th



So we have total of 13 elements from the above example. Now I need to list their relation ship too. Like

0th element is parent of 1 and 5.
1st element is parent of 2
2nd element is parent of 3 and 4
and so on

Answer

To generate the parent list, you could use a closure with a from variable, which holds the node number from where it has been called.

BTW, your list above is not correct for 5th element is parent of 6 and 10.

function loop(from) {			
    return function (a) {
        var f = i;
        if (from !== undefined) {
            linkDataArray.push({ from: from, to: i });
        }
        i++;
        if (Array.isArray(a.children)) {
            a.children.forEach(loop(f));
        }
    };
}

var data = [{ "name": "car", "value": "", "children": [{ "name": "v8_engine", "value": "", "children": [{ "name": "cylinder-arrangement", "value": "", "children": [{ "name": "type", "value": "string", "children": [] }, { "name": "max-elements", "value": "8", "children": [] }] }] }, { "name": "other-parts", "value": "", "children": [{ "name": "per-cylinder-parts", "value": "", "children": [{ "name": "piston-diameter", "value": "", "children": [{ "name": "type", "value": "uint32", "children": [] }, { "name": "range", "value": "2000... 9000", "children": [] }] }, { "name": "valves", "value": "", "children": [{ "name": "number", "value": "", "children": [] }, { "name": "position", "value": "", "children": [] }] }] }] }] }],
    i = 0,
    linkDataArray = [];

data.forEach(loop());

console.log(linkDataArray);

Comments