0

How do I get values from an object using dot notation when I have a variable that changes.

console.log(`${getData}.BUTTON_${item.UUID}`);
// outputs: [object Object].BUTTON_1 

In the object is a value for BUTTON_1. My item.UUID is a number that will change to a different button number. I need to be able to get the value for the button number

what works

console.log(getData.BUTTON_0);

My button number will be between 0 and 4 example getData.BUTTON_0, getData.BUTTON_1 ...

currently I need the button number to be a variable however the following does not work

console.log(`${getData}.BUTTON_${item.UUID}`);
Jerry seigle
  • 231
  • 2
  • 11
  • I would suggest rethinking whatever design pattern is leading you to dynamically created variable names. Maybe make an array of buttons and access them via `button[x]`? or if the uuid is not sequential like that, make buttons an object and give them a property, ie `button.uuid` – coppereyecat Jan 06 '22 at 19:43
  • Is there a particular reason you have to use dot notation? I'd use brackets instead. – mykaf Jan 06 '22 at 19:47