0

I haven't found an answer to this question on the site. How would I dynamically name arrays in javascript? I need to generate a number of arrays, the number of which is determined at run time. I am trying to make separate ajax requests that sends individual arrays to a php script for processing.

I have come up with this but it does not work:

 var 'objectArray'+id = [];
dave
  • 475
  • 6
  • 17

3 Answers3

2

The typical way would be to have an array of arrays.

var arrayOfArrays = [];
arrayOfArrays[id] = []; // add sub array

What you're asking for is a form of dynamic scoping and JavaScript does not support it. You can call the compiler and eval it but that's a pretty bad idea.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 2
    There is something really wrong about using artificial keys in an array despite the fact that it will not cause much problems in Javascript (if memory serves me right, it might cause problems though, it's not the way arrays are meant to be used). – David Mulder Jun 25 '15 at 23:24
  • @DavidMulder what makes you think `id` is not numeric? In every case I saw where people do this (typically people do this in the DOM) the id is actually also numeric). Of course, if you're using a regular string for a key you should use a `Map` object (and not an object). Appropriate data structure for the appropriate goal. – Benjamin Gruenbaum Jun 25 '15 at 23:24
  • 2
    It is probably numeric, but an array like `[undefined x 1234, [], undefined x 5678, [], etc.]` really is a mess. – David Mulder Jun 25 '15 at 23:25
  • I am trying to make separate ajax requests that sends individual arrays to php script. – dave Jun 25 '15 at 23:26
  • Yes, but why would you have an array like that? My first assumption is that ids are incrementing - this has been my experience when people have this issue. – Benjamin Gruenbaum Jun 25 '15 at 23:26
  • @dave you should probably make a single request and have the backend expect a 2 dimensional array (PHP has those too). If you have to use multiple requests, you probably want to use `Promise.all` and an array (and not an object that is not a data sturcutre). – Benjamin Gruenbaum Jun 25 '15 at 23:27
  • 2
    Because it was `id` and not `i` or `n`. `id` tends to be an `identifier` and that identifier is probably incremental in the database (or wherever it came from), but it's unlikely that the returning items are a set next to each other. – David Mulder Jun 25 '15 at 23:28
  • @DavidMulder why assume that? Logically if you have non-numeric values you'd want an iterable structure like a `Map`. The default for an ordered list should be an array. – Benjamin Gruenbaum Jun 25 '15 at 23:30
  • 1
    @BenjaminGruenbaum Wait, aren't you making the assumption here without justification? My assumption is 'I don't know what `id` contains, though I think the above as it's called `id`', your assumption is `I know what `id` contains, a set of subsequent integers`... I honestly think my assumption is a lot more sensible. – David Mulder Jun 25 '15 at 23:36
  • @DavidMulder your assumption is abusing an object as a map. My assumption is that values are consecutive which I have seen to be the case many times over when people were facing this issue. You copy pasted my answer after a minute which is bad enough as it is and now you downvoted my answer and are trash talking it. Talk about bad manners. – Benjamin Gruenbaum Jun 25 '15 at 23:39
  • 2
    @BenjaminGruenbaum First of all your comment is extremely offensive because you're accusing me of having copied content. Something I did not do. For that matter, I didn't even read your answer till I mostly finished writing mine. Secondly, I waited with downvoting till I realized that you weren't going to fix the flaw in your answer that I openly pointed out. Lastly `map`'s were only introduced in EC6, so I have no idea why you're bringing those up, but in all previous versions of JS you would use objects in these kind of cases. – David Mulder Jun 25 '15 at 23:43
  • And you bring a friend, great, really classy. I'm done here, I have better ways to spend my time. – Benjamin Gruenbaum Jun 25 '15 at 23:45
  • @BenjaminGruenbaum Wait, now it's my fault that someone agreed with me? :S Instead of apologizing for your behaviour you're going to add even more accusations? – David Mulder Jun 25 '15 at 23:47
1

Easiest thing is just creating an object and setting them in it

var objectArrays = {};
objectArrays[id] = [];

The way to do literally what you want is finding the scope you're currently in and setting it in it or using eval.

David Mulder
  • 26,123
  • 9
  • 51
  • 114
0

A good way is to use an array of arrays as suggested:

var objectArray = [];
objectArray[id] = [];

If you do not want to use your own array, you can use window object which holds the window global vars in order to store it there as a value as a global var (not a good practice though):

window["objectArray" + id] = [];

Finally, a (bad in this case) alternative way of doing it is by using eval (which is a way to evaluate an expression at runtine in JavaScript in a sense):

eval("var objectArray" + id + " = [];");
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54