第一次记代码
想了很久,应该会比较绕,记录下来,目前可以实现,等以后熟悉了之后再看下多写点注释在代码上。
代码段如下:
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
/*
[
{id: 1, label: '陈小春',title:'演员', shape: 'circularImage', image:"images/test/person.png",
properties:{"姓名":"陈小春", "国籍":"中华人民共和国", "性别":"男", "婚姻状况":"已婚","民族":"汉族", "身高":"175CM", "兵役状况": "未服兵役"}
},
{id: 2, label: '应采儿', shape: 'circularImage', image:"images/test/person2.png",
properties:{"姓名":"应采儿", "国籍":"中华人民共和国", "性别":"女", "婚姻状况":"已婚", "民族":"汉族", "身高":"160CM", "出生日期":"1978年5月"}
},
{id: 3, label: 'Jasper', title:'陈小春与应采儿儿子',shape: 'circularImage', image:"images/white/layout/icon/man.png",
properties:{"姓名":"Jasper", "国籍":"中华人民共和国", "性别":"男", "婚姻状况":"未婚","民族":"汉族", "身高":"100CM", "爱好":"吃东西"}
}
]
*/
// 代码实现
let nodes = relationAnalysis_canvas.nodes;
// 获取选中节点的相同标签和独有标签,假设选中以上节点
let allProps = [];
$.each(nodes, function (index, node) {
let prop = node.properties;
allProps.push(prop);
});
let sameProp = new Map(), specialProp = new Map(); // 相同标签和独有标签
// 循环 A 遍历属性
for(let p in allProps[0]) { // 第一个属性对象 2
for(let p2 in allProps[1]){
if(p === p2){ // key相同
sameProp.set(p, 1);
if(specialProp.has(p)){
specialProp.delete(p);
}
}else{
if(!sameProp.has(p2) && !sameProp.has(p)){
specialProp.set(p, 2);
specialProp.set(p2, 2);
}
}
}
}
for(let prop2 = 2; prop2 < allProps.length; prop2++){
for(let p2 in allProps[prop2]){
if(sameProp.has(p2)){ // 还有共同属性
sameProp.set(p2, 1);
}else {
sameProp.delete(p2);
specialProp.set(p2, 2);
}
}
}
console.log('相同key', sameProp);
console.log('不同key', specialProp);
for([keyStr, vlaStr] of sameProp.entries()){
let tmpValue = '', count = 0;
$.each(allProps, function (index, prop){
for(let p in prop){
if(keyStr == p){
if(count === 0){
tmpValue = prop[p];
}
if(prop[p] == tmpValue){
count ++;
}
}
}
});
if(count === allProps.length){
sameProp.set(keyStr, tmpValue);
}
tmpValue = '';
count = 0;
}
sameProp.forEach(function (element, index, array){
console.log(index + " : " + element + " : " + array);
if(element === 1){
specialProp.set(index, 2);
sameProp.delete(index);
}
});
console.log('相同key相同value', sameProp);
console.log('不同key', specialProp);