我尝试通过编程从下面提供的sampleData中以邻接表的形式生成一个新对象,以用于jointJS,因此最终它会生成组织结构图:
sampleData = [
{"id":"1224286", "label":"someLabel1", "image":"male.png", "level":"1"},
{"id":"1166041", "label":"someLabel2", "image":"male.png","refparent":"1224286", "level":"2"},
{"id":"1224376", "label":"someLabel3", "image":"male.png","refparent":"1224286", "level":"2"},
{"id":"1317500", "label":"someLabel4", "image":"male.png", "refparent":"1224286", "level":"2"},
{"id":"1232930", "label":"someLabel5", "image":"male.png", "refparent":"1166041", "level":"3"},
{"id":"1163148", "label":"someLabel6", "image":"male.png", "refparent":"1317500", "level":"3"},
{"id":"1279087", "label":"someLabel7", "image":"male.png", "refparent":"1166041", "level":"3"},
{"id":"1185740", "label":"someLabel8", "image":"male.png","refparent":"1224376", "level":"3"},
{"id":"1234273", "label":"someLabel9", "image":"male.png", "refparent":"1232930", "level":"4"},
{"id":"1210593", "label":"someLabel10", "image":"male.png", "refparent":"1185740", "level":"4"},
{"id":"1279247", "label":"someLabel11", "image":"male.png", "refparent":"1185740", "level":"4"},
{"id":"1316785", "label":"someLabel12", "image":"male.png", "refparent":"1232930", "level":"4"},
{"id":"1266325", "label":"someLabel13", "image":"male.png", "refparent":"1210593", "level":"5"}
];
应该按照以下结构转换sampleData,以将其用作邻接表:
var list = {
'1224286': ['1166041', '1224376', '1317500'],
'1166041': ['1232930', '1279087'],
'1224376': ['1185740'],
'1317500': ['1163148'],
'1232930': ['1234273', '1316785'],
'1163148': [],
'1279087': [],
'1185740': ['1210593', '1279247'],
'1234273': [],
'1210593': ['1266325'],
'1279247': [],
'1316785': [],
'1266325': []
};
它看起来很简单,但我看不到它。
最佳答案
试试这个代码(复杂度为n * n)
var output = {};
sampleData.forEach(function(val){
var id = val.id;
var arr = [];
sampleData.forEach(function(innerVal){
if ( innerVal.refparent == id )
{
arr.push(innerVal.id);
}
});
output[id] = arr;
});
console.log(output);
否则您可以转换数据结构以获得更好的性能
var transformed = {};
sampleData.forEach(function(val){
var ref = val.refparent;
transformed[ref] = transformed[ref] || [];
transformed[ref].push(val.id);
});
//现在迭代原始列表以查看遗漏了什么
var output = {};
sampleData.forEach(function(val){
output[val.id] = transformed[val.id] || [];
});
这给您带来2n的复杂度。
演示
var sampleData = [
{"id":"1224286", "label":"someLabel1", "image":"male.png", "level":"1"},
{"id":"1166041", "label":"someLabel2", "image":"male.png","refparent":"1224286", "level":"2"},
{"id":"1224376", "label":"someLabel3", "image":"male.png","refparent":"1224286", "level":"2"},
{"id":"1317500", "label":"someLabel4", "image":"male.png", "refparent":"1224286", "level":"2"},
{"id":"1232930", "label":"someLabel5", "image":"male.png", "refparent":"1166041", "level":"3"},
{"id":"1163148", "label":"someLabel6", "image":"male.png", "refparent":"1317500", "level":"3"},
{"id":"1279087", "label":"someLabel7", "image":"male.png", "refparent":"1166041", "level":"3"},
{"id":"1185740", "label":"someLabel8", "image":"male.png","refparent":"1224376", "level":"3"},
{"id":"1234273", "label":"someLabel9", "image":"male.png", "refparent":"1232930", "level":"4"},
{"id":"1210593", "label":"someLabel10", "image":"male.png", "refparent":"1185740", "level":"4"},
{"id":"1279247", "label":"someLabel11", "image":"male.png", "refparent":"1185740", "level":"4"},
{"id":"1316785", "label":"someLabel12", "image":"male.png", "refparent":"1232930", "level":"4"},
{"id":"1266325", "label":"someLabel13", "image":"male.png", "refparent":"1210593", "level":"5"}
];
var transformed = {};
sampleData.forEach(function(val){
var ref = val.refparent;
transformed[ref] = transformed[ref] || [];
transformed[ref].push(val.id);
});
var output = {};
sampleData.forEach(function(val){
output[val.id] = transformed[val.id] || [];
});
document.body.innerHTML += JSON.stringify(output,0,4);
关于javascript - JSON数据的邻接表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36662533/