JSFiddle - React, Tailwind, and code Playground
by Ben Noble
HTML
<div id='output'></div>
JavaScript
var inJSON = {
"employee": [{
"seq": "1",
"sourceTable": "SOURCE_TABLE1",
"targetTable": "TARGET_TABLE"
},
{
"seq": "1",
"sourceTable": "SOURCE_TABLE2",
"targetTable": "TARGET_TABLE"
}
]
};
inJSON = inJSON.employee;
var outJSON = {
"nodes": [],
"links": []
};
var tempNodesArray = [];
var tempNodesJSON = {
"id": null,
"label": null,
};
var tempLinksArray = [];
var tempLinksJSON = {
"from": null,
"to": null
};
function searchArray(table_name){
for(var i = 0; i < tempNodesArray.length; i++) {
if(tempNodesArray[i].id == table_name) {
return i;
}
}
}
for (itms of inJSON) {
tempLinksJSON["from"] = itms['sourceTable'];
tempLinksJSON["to"] = itms['targetTable'];
tempLinksArray.push(tempLinksJSON);
//outJSON.links.push(tempLinksArray);
if (typeof searchArray(tempLinksJSON["from"]) == 'undefined')
{
tempNodesJSON["id"] = tempLinksJSON["from"];
tempNodesJSON["label"] = tempLinksJSON["from"];
tempNodesArray.push(tempNodesJSON);
}
tempNodesJSON = {
"id": null,
"label": null
};
if (typeof searchArray(tempLinksJSON["to"]) == 'undefined')
{
tempNodesJSON["id"] = tempLinksJSON["to"];
tempNodesJSON["label"] = tempLinksJSON["to"];
tempNodesArray.push(tempNodesJSON);
}
tempNodesJSON = {
"id": null,
"label": null
};
//tempLinksArray = [];
tempLinksJSON = {
"from": null,
"to": null
};
}
//for (node of tempNodesArray) {
//tempNodesJSON.key = node;
//outJSON.nodes.push(tempNodesJSON);
//tempNodesJSON = {
// "key": null,
// "color": "orange"
//};
//}
//console.log(outJSON);
var div = document.getElementById('output');
div.innerHTML += JSON.stringify(tempNodesArray);
//div.innerHTML += tempNodesArray;