0

I have following javascript variable:

this.Edges = {};

Then I did

this.Edges[path] = {edgeDst};

So Edges variable at this point is somehing like

Edges['232kb32i23'] = { edgeDst: 'AND_m_5' }

after that I when I did

this.Edges[path] = {edgeSrc};

It overwrote the value I had added with edgeDst. At this point Edges is like following:

Edges['232kb32i23'] = { edgeSrc: 'B_m_5' }

But I want to produce something like: Edges['232kb32i23'] = { edgeDst: 'AND_m_5', edgeSrc: 'B_m_5' }

Here I can't added edgeSrc and edgeDst simulataneously.

How will I achieve this?

5
  • How do you get both keys and values from doing = {edgeSrc}; , what's in edgeSrc etc ? Commented Nov 8, 2016 at 0:16
  • 2
    @adeno - ES2015 or ES2016 has the answer to that Commented Nov 8, 2016 at 0:18
  • @JaromandaX - That's what I suspected, hence my question ? Commented Nov 8, 2016 at 0:19
  • if you want to add both at the same time, this.Edges[path] = {edgeDst, edgeSrc}; Commented Nov 8, 2016 at 0:20
  • Okay then, if the answer below worked, I guess it wasn't property shorthand after all. Commented Nov 8, 2016 at 0:23

2 Answers 2

4

You can use the following:

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']);

You will have to modify the code to use this for your application, but I tried to make as succinct an example as possible.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use the new ES6 syntactic sugar. It looks like you can do something like this:

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"} 

Here is a link to a functioning example: https://jsfiddle.net/xwf3wadr/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.