0

I wonder if anyone can help with this JavaScript problem that I can't find a solution to.

I have a number of pre-defined objects as shown below. The details are unimportant to this example apart from the names of the objects.

const dialText = {
    id: 'dialText',
    afterDatasetsDraw(chart, args, options){
        ...
    }
}
const gaugeNeedle = {
    id: 'gaugeNeedle',
    afterDatasetsDraw(chart, args, options){
    ... etc.
   }
}
const gaugeOutline = {
   id: 'gaugeOutline',
   afterDatasetsDraw(chart, args, options){
   ... etc.
   }
}
etc....

In a separate code segment I have an array that includes a list of some object names (corresponding to these pre-defined objects)

var myList = ['dialText', 'gaugeNeedle'];

and a separate object called chartData which looks something like this:

var chartData = {type:"dot", x:20, y: 30, color:"white"};

I now want to include the objects named in myList into the chartData object as an attribute called 'plugins'.

I hoped to achieve this by the following code:

for (var myValue in myList ) {
   if (typeof chartData.plugins !== 'undefined'){
       chartData.plugins.push(myValue);
   } else {
      chartData.plugins = [myValue];
   }
}

However, this approach does not work because the names are going into chartData object rather than the objects themselves. Is there some way to add the objects referenced by name (in myList) to the chartData object as objects.

The final chartData array should end up looking something like this:

chartData = {
  type:"dot", 
  x:20, 
  y: 30, 
  color:"white",
  plugins: {
      [id: 'dialText',
        afterDatasetsDraw(chart, args, options){
        ...
      }],
      [id: 'gaugeNeedle',
        afterDatasetsDraw(chart, args, options){
        ...
      }]
  }
}

Any suggestions will be much appreciated.

Clinton
  • 1,111
  • 1
  • 14
  • 21
  • `myValue` is a string, not the object in the variable with that name. – Barmar Aug 10 '22 at 15:38
  • Your loop is equivalent to `chartData.plugins = [...myList]` – Barmar Aug 10 '22 at 15:39
  • Just curious: does the `'guageNeedle '` in `myList` refer to `guageNeedle` or `gaugeOutline`, as they both have an id of `'guageNeedle'`? – Heretic Monkey Aug 10 '22 at 15:40
  • 1
    You should get rid of the quotes in `myList`: `var myList = [dialText, gaugeNeedle];` so you have a list of objects, not a list of names. – Barmar Aug 10 '22 at 15:42
  • 1
    @Barmar is correct. If you need the array myList to contain string values for some comparison reason, you should create a master object and store each of your listed objects as an object within the master object. – sychordCoder Aug 10 '22 at 15:56
  • @Barmar thank you for your observation, I should have noted that the myList variable has to be a list of the object names and cannot be the objects themselves as these define which objects (by name) should be added to the chartData object. The array in myList may even be passed as an argument to the main function. – Clinton Aug 10 '22 at 16:03
  • Great idea @sychordCoder that may be a way to achieve this. – Clinton Aug 10 '22 at 16:05
  • @HereticMonkey My bad, sorry, they should each have had different id's - I have fixed this now. – Clinton Aug 10 '22 at 16:07
  • 1
    See https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript regarding dynamic variable access. But using the names as keys in a container object is better. – Barmar Aug 10 '22 at 18:11

0 Answers0