只需添加color
属性
工作演示
const graph = {
nodes: [
{ id: 'Node 1', label: "Node 1", title: "node 1 tootip text" ,first:true, color: "red" },
{ id: 2, label: "Node 2", title: "node 2 tootip text" },
{ id: 3, label: "Node 3", title: "node 3 tootip text" },
{ id: 4, label: "Node 4", title: "node 4 tootip text" },
{ id: 5, label: "Node 5", title: "node 5 tootip text",last:true, color: 'Green' }
],
edges: [
{ from: 'Node 1', to: 2,label:'dddddd' },
{ from: 'Node 1', to: 3,label:'adasd' },
{ from: 2, to: 4 },
{ from: 2, to: 5 }
]
};
或者您可以在 first 和 last 属性的基础上动态更改它
let obj = { nodes: [
{ id: 'Node 1', label: "Node 1", title: "node 1 tootip text" ,first:true},
{ id: 2, label: "Node 2", title: "node 2 tootip text" },
{ id: 3, label: "Node 3", title: "node 3 tootip text" },
{ id: 4, label: "Node 4", title: "node 4 tootip text" },
{ id: 5, label: "Node 5", title: "node 5 tootip text",last:true }
] };
let updatedNodes = obj.nodes.map(( o ) => {
let {first, last} = o;
first ? o.color = 'red' : last ? o.color = 'green' : null;
return {...o}
});
console.log(updatedNodes);