0

I would like to set one array equal to another via a string. I have the following array:

var apartment234 = ["01.07.2017","02.07.2017","03.07.2017","04.07.2017","05.07.2017","06.07.2017","07.07.2017"];

And I have the following string which equals the array above (apartment234).

unavailable = "apartment" + ausgewzimmer;

If I now want a function to return something based on the array as shown below, it doesn't return anything.

return !!(unavailable.indexOf(date.format('DD.MM.YYYY')) > -1);

However, if I use the array directly, it does work.

return !!(apartment234.indexOf(date.format('DD.MM.YYYY')) > -1);
//returns desired values

I am not sure what I am doing wrong... unavailable does equal apartment234 as in unavailable = "apartment234". Why doesn't it function? Could you please help me?

Moritz
  • 745
  • 1
  • 10
  • 32
  • 1
    This doesn't seem like the best way to do it. What exactly are you trying to do? – Aron May 24 '17 at 10:24
  • 5
    Possible duplicate of [Use dynamic variable names in JavaScript](https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript) – George May 24 '17 at 10:24
  • 1
    `apartment234` looks like something that should be indexed: `apartment[234] = [...]` so that its easy to look up by index ... – Alex K. May 24 '17 at 10:26
  • @Aron `ausgewzimmer` depends on a selection HTML element or rather it's value... consequently, I have numerous arrays named according to the value of `unavailable` and would like to use strings in the function or more precisely in the return... – Moritz May 24 '17 at 10:28
  • @George sorry, I couldn't find it :/ thank you – Moritz May 24 '17 at 10:36

3 Answers3

2

Define a object with property "apartment234", then it can be accessed using bracket notation.

var obejct = {
  "apartment234": ["01.07.2017", "02.07.2017", "03.07.2017", "04.07.2017", "05.07.2017", "06.07.2017", "07.07.2017"]
};

var unavailable = "apartment" + 234;
console.log(obejct[unavailable]);
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

The string "apartment234" is not the same as the array instance apartment234.

When you do this:

unavailable.indexOf(....)

You are just calling the indexOf() method on the string.

You could use the eval() method to accomplish this:

unavailable = eval("apartment" + ausgewzimmer);

eval() evaluates, or executes the given string as JS code.

HaukurHaf
  • 13,522
  • 5
  • 44
  • 59
  • 1
    I wouldn't recommend this approach when it can be done without calling `eval()` via `window[unavailable]` and we don't know how `ausgewzimmer` is getting created. – George May 24 '17 at 10:28
  • Why not? It's created via `$("#element").val()` from an HTML... why wouldn't you use it? And is it ok to use it here? @George – Moritz May 24 '17 at 10:38
  • 1
    @Moritz `eval()` [has problems](https://24ways.org/2005/dont-be-eval) with performance and with people being able to inject code into your js. `eval()` isn't always evil but (to me) it should normally be a last resort. – George May 24 '17 at 10:46
  • 1
    eval() is usually the "easy" way to accomplish things like this, but like the others have pointed out, there are other ways which are better. I usually don't use eval(), I try to do things differently. I thought it was OK to mention here as an easy option. – HaukurHaf May 24 '17 at 10:48
  • 1
    @HaukurHaf eval is useful in this case if you don't know what scope the object is on :) – George May 24 '17 at 10:50
0

If you only need to have string-representation of your array as return of function calls, can't you just do regular Array.toString() and String.split(",") methods to handle this?