0

I am not sure if my question title makes sense. Basically I have a number of objects and want to reference a specific one based on a variable.

For example I have these objects in a store called data using Pinia:

var obj1 = ref([{name:"Doug",title:"main character"},{name:"Porkchop",title:"dog"}]);
var obj2 = ref([{name:"Patty",title:"Other character"},{name:"Skeeter",title:"friend"}]);

My code looks at a variable based on the route param/URL, and I want to use that to pick which object from above.

var myVar = "obj2"; <- this is an example and is dynamic based on URL updated in a function when mounted.

Then I want to loop one of the objects like below:

  Object.keys(data.obj1).forEach((k, i) => {
    formData.append(data.obj1[k].name, data.obj1[k].title);
  });

I cannot figure out how to dynamically change the data.->HERE<- to what I need. I have tried dynamically setting the property (if that is the correct term.).

data.myVar    
data.[myVar]

For clarification, I am not trying to modify the values of anything. I just want to change the reference of the object based on a variable.

I just want to avoid hard coding a switch statement or something... if possible.

Thank you, and let me know if you need clarification or the examples don't make sense.

Eric.18
  • 463
  • 4
  • 20
  • @derpirscher, no. I don't want to change the value of anything. I just want to use the variable to change the reference of the object. – Eric.18 Sep 21 '22 at 20:08
  • You want `data[myVar]` It doesn't matter whether you're modifying or accessing. – Barmar Sep 21 '22 at 20:13
  • @Barmar, I thought so too but I get an error in the loop ("Uncaught (in promise) TypeError: Cannot convert undefined or null to object". ) Even if I hardcode myVar = "obj1", then console.log(myVar) I get "obj1" then I log data[myVar] to check the value I get undefined... that does not make sense. I think. – Eric.18 Sep 21 '22 at 20:21
  • Where do you create `data`? Please post a [mre] – Barmar Sep 21 '22 at 20:26
  • If you do `data.ref1 = ref1; data.ref2 = ref2;` then it should work. – Barmar Sep 21 '22 at 20:28
  • @Barmar, here is a simple fiddle. I just want the object reference to be dynamic. https://jsfiddle.net/eg4wf2y5/ – Eric.18 Sep 21 '22 at 20:46
  • You can use `window[myVar]` to access global variables dynamically. But it would be better to put all the objects into another object. – Barmar Sep 21 '22 at 20:52
  • See https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript – Barmar Sep 21 '22 at 20:53
  • Your question keeps saying "object property", but you have variables that aren't object properties. – Barmar Sep 21 '22 at 20:53
  • Well, if you keep talking about *object properties* and `data.myvar`, `data.[myvar]` and `data.obj1`, then no wonders people assume, you want to dynamically access a property of the object `data`. So instead of insulting people who are trying to help you, maybe you rephrase your question, so that it expresses what you really have and what you really need ... – derpirscher Sep 22 '22 at 08:10
  • @derpirscher, Barmar, Sorry for the confusion, I am not trying to insult anyone, that mixup was my mistake. I guess I don't know what I am asking. Again, I am using Vue with a store that has ref objects in it so I want to select the one I need based on a variable. I tried using data.window[myVar] but i still get an error (Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'question'). This situation is new to me so I am a bit lost on how to dynamically change the value of something in the function... – Eric.18 Sep 22 '22 at 12:51

0 Answers0