来自 JSON 数据的邻接表
adjacency list from JSON data
我尝试从以下提供的 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"}
];
样本数据应按照以下结构进行转换,以将其用作邻接表:
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);
我尝试从以下提供的 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"}
];
样本数据应按照以下结构进行转换,以将其用作邻接表:
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);