我有以下javascript变量:
this.Edges = {};
然后我做了
this.Edges[path] = {edgeDst};
所以此时的Edges变量就像
一样边缘['232kb32i23'] = {edgeDst:'AND_m_5'}
之后我做了
this.Edges[path] = {edgeSrc};
它覆盖了我用edgeDst添加的值。 在这一点上,Edges就像是:
边缘['232kb32i23'] = {edgeSrc:'B_m_5'}
但是我想要产生这样的东西: Edges ['232kb32i23'] = {edgeDst:'AND_m_5',edgeSrc:'B_m_5'}
这里我无法同时添加edgeSrc和edgeDst。
我将如何实现这一目标?
答案 0 :(得分:4)
您可以使用以下内容:
var Edges = {}; // these are just so that you can run the snippet
var path = '232kb32i23';
Edges[path] = {}; // set Edges[path] to a new object
Edges[path].edgeDst = 'AND_m_5'; // set the edgeDst property on Edges[path]
Edges[path].edgeSrc = 'B_m_5'; // set the edgeSrc property on Edges[path]
console.log(Edges); // run the snippet to see the result
console.log(Edges['232kb32i23']);

您必须修改代码才能将this
用于您的应用程序,但我尽量做一个简洁的示例。
答案 1 :(得分:0)
如果你想使用新的ES6语法糖。看起来你可以这样做:
this.Edges = {};
var path = 'the_path',
edgeDst = 'edgeDst',
edgeSrc = 'edgeSrc';
this.Edges[path] = { edgeDst, edgeSrc };
console.log(this.Edges) => {edgeDs:"AND_m_5",edgeSrc:"edgeSrc"}
以下是功能示例的链接:https://jsfiddle.net/xwf3wadr/