0

I want to create an array from a function. How would I go about doing this so that the array name is passed within the parameter.

function arrayCreate(name){
     name = [];
    }

So if I used arrayCreate("Hello") I would have Hello = [] as an array. The obvious problem is I'm passing a string so how would I overcome this.

To explain a bit more about my problem I have a function which updates a chart as seen below:

      self.updateChart = function(name){

        if (typeof chartData == 'undefined') {
          chartData = [];
        }
        chartData.push({
          date: game.tickCount,
          visits: self.price,
        });
        priceLabour.validateData();
        return chartData;
        }

Now the "chartData = []" and "priceLabour" I would like for it to be dynamic and change based on the "name" parameter.

epayne
  • 149
  • 1
  • 7
  • 1
    even if you get it, what do you do with the named array? – Nina Scholz Feb 20 '17 at 16:21
  • I want to display different charts which require different named arrays – epayne Feb 20 '17 at 16:23
  • and how do you hand over the data? – Nina Scholz Feb 20 '17 at 16:25
  • I have an Object called Market.list which to create a graph I use Market.list[4].updateChart();. Obviously it's fine for one market but I want to re-use the code – epayne Feb 20 '17 at 16:27
  • It doesn't look like it should matter what the array is named since it's only being referenced inside this function. Why do you need to specify a name? – shanzilla Feb 20 '17 at 16:29
  • Because for amChart it takes in a dataProvider called chartData. I want to use the same chart but I can't use chartData array again – epayne Feb 20 '17 at 16:33
  • Can you provide a code example of where else you need to use the chartData array? I'm still not seeing why you need to pass in a name. – shanzilla Feb 20 '17 at 16:35
  • I'm using this library https://www.amcharts.com/demos/line-chart-with-scroll-and-zoom/ if you click on the source code you can see "chartData" – epayne Feb 20 '17 at 16:38
  • where do you call `arrayCreate`? – Nina Scholz Feb 20 '17 at 16:56

2 Answers2

1

You can't just create variables by a string. You can create, assign, and read properties on objects using a string:

var obj = {};
obj[name] = [];
obj[name].push(...);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

You can create a dynamically named array as a property of an object. Not entirely sure what your needs are but here's an example based on your provided code.

  var data = {};

  self.updateChart = function(name){
    // Store a shallow clone of the chartData array in the data obj
    data[name] = chartData.slice(0);

    if (typeof data[name] == 'undefined') {
      data[name] = [];
    }
    data[name].push({
      date: game.tickCount,
      visits: self.price,
    });
    priceLabour.validateData();
    return data[name];
  };

After calling updateChart you should be able to use data["myString"] elsewhere to refer to your array.

shanzilla
  • 467
  • 4
  • 16