假设我有这些数组:
var nodes = [v1,v2,v3,v4,v5];
var edges = [{source: v1, target: v2},{source: v2, target: v3},{source: v3, target: v5}];
还有一个对象:
var graph = {};
我想将“节点” 和“边缘” 附加到“图形” 对象中。 所以最后应该是这样的:
graph = { nodes: [v1,v2,v3,v4,v5], edges: [{source: v1, target: v2},{source: v2, target: v3},{source: v3, target: v5}] };
请注意,数组名称(节点和边)位于我的“图形”对象内。 谢谢
答案 0 :(得分:1)
像这样初始化您的对象
var graph = {nodes: nodes, edges: edges};
或设置像这样的值
var graph = {};
graph.nodes = nodes;
graph.edges = edges;
答案 1 :(得分:1)
$(document).ready(function(){
var nodes = ['v1','v2','v3','v4','v5'];
var edges = [{source: 'v1', target: 'v2'},{source: 'v2', target: 'v3'}, {source: 'v3', target: 'v5'}];
var graphs={nodes:nodes,edges:edges};
//method 1
console.log(JSON.stringify(graphs));
//method2
console.log(JSON.stringify({nodes,edges}));
});
使用上面显示的任何方法
答案 2 :(得分:1)
v1= "test1";
v2= "test2";
v3= "test3";
v4= "test4";
v5= "test5";
var nodes = [v1,v2,v3,v4,v5];
var edges = [{source: v1, target: v2},{source: v2, target: v3},{source: v3, target: v5}];
var graph ={};
graph.edges =edges;
graph.nodes =nodes;
console.log(graph);