0

I need to dynamically store pairs of values in a object. I have a code similar to:

obj = {};

avariable = "Name x";
anothervariable = 100;
var name = avariable;
var value = anothervariable;

obj[name] = value;

It does not work, the console.log shows: obj = [object Object]

I tried to look at this answer with a very similar problem, but I do not know in advance the "name" so I cannot use:

obj['name y']

My final goes is to store a value for each name and to restore those values later on with a loop given the name.

Filippo
  • 320
  • 2
  • 5
  • 22
  • Out of curiosity, what does `console.dir(obj)` yield? – esqew Sep 24 '20 at 03:50
  • `obj[name] = value` should work. You've probably just not logged it correctly to see it there. – Andrew Li Sep 24 '20 at 03:51
  • I think the question is misleading. array has no key value pair, it goes with an index. I think your refers to creating an key value pair object using bracket notation. – elpmid Sep 24 '20 at 03:53
  • 1
    `console.log(obj)` and `console.dir(obj)` and working fine for me. – Cody Pace Sep 24 '20 at 03:54
  • You also should avoid bad practice in using `var name` in the Window scope as it will attempt to override the preexisting [`Window.name`](https://developer.mozilla.org/en-US/docs/Web/API/Window/name) in browser environments. – esqew Sep 24 '20 at 03:54
  • 1
    Like @CodyPace, I too cannot reproduce in [my Repl.it](https://repl.it/@esqew/PlushEsteemedLifecycle#script.js). – esqew Sep 24 '20 at 03:55
  • 1
    is what you tried is to alert obj? Thats the only way it shows [Object Object] – Beingnin Sep 24 '20 at 03:56
  • whatever the case you most likely need to convert the object to a string to keep from seeing [object Object]. `alert(JSON.stringify(obj))` or `console.log(JSON.stringify(obj))` – Cody Pace Sep 24 '20 at 04:00
  • Thank you all! I was logging other variables and that is why it was not showing correctly, but both console.dir(obj) and console.log(JSON.stringify(obj)) show that the values are added correctly to obj. – Filippo Sep 24 '20 at 10:54
  • You should put more effort into _properly_ describing your problems. _“It does not work, the console.log shows […]”_ - that part, _how_ you are actually logging the values, was the most relevant here, but specifically _that_ you failed to show us. – 04FS Sep 24 '20 at 10:59
  • I did not know it was relevant, my code was different in other parts too. All the answers above made me understand the issue, so they were helpful even if I did not include the most relevant info. – Filippo Sep 24 '20 at 11:05

1 Answers1

0

Thank you for the answers, I summarized below:

The Code above is correct. I was concatenating multiple variables and that is why I was getting [object Object].

console.log("callback called, flag: "+ flagq+", obj = "+obj);

console.log(obj), console.dir(obj) and console.log(JSON.stringify(obj)) show that the values are added correctly to obj.

Filippo
  • 320
  • 2
  • 5
  • 22
  • It was not actually because you logged multiple values - that works fine without any problem - but because you _concatenated_ multiple values into a single string. – 04FS Sep 24 '20 at 10:56