title: Calling a method within a dynamically named object
body: <p>I am trying to invoke a method of a dynamically named object.</p>
  
  <p>I have a few objects each containing a method named 'getWeight'. I need to invoke this method of a subset of these objects. However, the subset of objects depends on user inputted info, which is why I'm attempting to dynamically construct the object name and invoke it's 'getWeight' method within a loop.</p>
  
  <p>My code is below:</p>
  
  <pre><code>// Importing objects containing 'getWeight' method
  const Apple = require('../classses/Apple');
  const Pear = require('../classes/Pear);
  const Carrot = require('../classes/Carrot);
  const Potato = require('../classes/Potato);
  
  const promises = {};
  const user = 'userX';  // This is the inputted info, could also equal 'userY'.
  const searchableFoods = {
    userX: ['Apple', 'Pear'],
    userY: ['Carrot', 'Potato']
  };
  
  for (i = 0; i &lt; searchableFoods[user].length; ++i) {
    promises[searchableFoods[user][i]] = new Promise(function(resolve, reject) {
      // Below line should behave like: Apple.getWeight(arg1, function....
      searchableFoods[user][i].getWeight(arg1, function(response) {
        resolve(response);
      })
    })
  }
  </code></pre>
  
  <p>Unfortunately, I get this error:</p>
  
  <pre><code>[searchableFoods[user][i].getweight] is not a function
  </code></pre>
  
  <p>I've tried a number of variations but can't make it work. Is it possible to do this?</p>