1

if I have:

x=3;
FirstNames = ["John","Paul","George","Ringo"];
LastNames = ["Smith","Jonson","Jones","Doe"];
SemiRandomNumbers= [3,6,7,4,2,1,0,9];

I can set: ArrayToUse = FirstNames; alert(ArrayToUse[x]); // > "Ringo"

I know it's weird, but it's how I set it up. My problem lies in:

ArrayToUse DOES NOT = FirstNames, It = "John","Paul","George","Ringo"

I want to: alert( "The Array used Was: " +ArrayToUse+ "." ); and I want the result to be:

"The Array Used Was: FirstNames."

I don't want the result:

"The Array Used Was: John","Paul","George","Ringo."

SO...... I know it sounds silly, but, is there a predefined property of an object that contains the name of the object?

i.e. ArrayToUse.name -> FirstNames. ??? (currently undefined)

is there a property I am unaware of for this use? or, would I have to set it up on my own like: FirstNames = ["John","Paul","George","Ringo"]; FirstNames.name="FirstNames";

I hate to go through the trouble/filespace/bandwidth if it already exists.

Before anyone says: "Answered elsewhere" I admit I HAVE seen similar questions, BUT.. after several hours, I keep finding the answer to work backwards. i.e. the poster tells you how to make a NEW variable with the name of the variable being the string contents of the array, and the poster warns them to ONLY use existing arrays. This is the EXACT OPPOSITE of what I am looking for/asking.

I want to grab the NAME of an object/array in string form, NOT the contents of the array/object.

Ronk
  • 215
  • 2
  • 11
  • What you are asking is impossible. ECMAScript variables exist as properties of an environment object that you can't access. Also, values do not know which which variable or properties reference them: there might be one, many or none. – RobG Feb 08 '16 at 05:14
  • 1
    When you set ArrayToUse = FirstNames, both ArrayToUse and FirstNames are pointers to the same memory location and are identical, there is no way for the computer to tell which one was the "original" so the answer to your question is No there is no magic property you can use and yes you have to do it manually. – Jason Fetterly Feb 08 '16 at 05:23
  • So,Jason, FirstNames = ["John","Paul","George","Ringo"]; FirstNames.name="FirstNames"; would be the only way to do it? – Ronk Feb 08 '16 at 05:37
  • 1
    I don't think this is a duplicate of the linked question. Given the OP is asking specifically about arrays (not *any* variables) it would be easy enough to solve the problem by adding a `First names.name="Firstnames"` property, though it would probably be better to restructure Firstnames to be an object with a `name` string property and `data` array property. – nnnnnn Feb 08 '16 at 05:41
  • Rob, I DID try to address differences, but, do you think I should add my question as an answer to someone else's question?(I thought it would be wrong, or rude?) If I added it as a comment, it would be rejected for being too long.. and would not get across the information I am trying to ask. I did however set examples to uses, like no one else has done yet. – Ronk Feb 08 '16 at 05:42
  • nnnnnn- First off, thanks. The purpose of the seperation of arrays was to keep the directory structure clean, each "Person" will have a pic associated, and the*name* is used as part of the string construction of the path. As some of my Arrays have upwards of 300+ entries (one is almost 1k) the pictures are bogging down the server's indexing speed having 10k+ images dumped into one directory. Editing is also a pain a this high capacity. – Ronk Feb 08 '16 at 05:48
  • @nnnnnn—I think all the approaches suggested here are in answers to the duplicate. – RobG Feb 08 '16 at 06:09
  • 1
    @RobG - well, the duplicate asked about variables in general and had variables with primitive values, as compared to multiple variables referencing the same objects. Seems similar on the surface, but the underlying goal is quite different. The duplicate doesn't really have a practical, generic solution that works everywhere, but that doesn't matter here where what this OP really has is a data structure issue masquerading as a question about variables. – nnnnnn Feb 08 '16 at 06:22
  • @RobG, also, we have the allowable comment size problem. Can you allow me to use 10x the usual available character space for telling them WHY and HOW my question is different... Also I see NONE of the same solutions being tried there. – Ronk Feb 08 '16 at 06:27
  • The link you provided also has people saying it is a duplicate question. Will it be deleted as well? – Ronk Feb 08 '16 at 06:31
  • @RobG, again. It also asks about "Creating a FUNCTION to LOG to the console" the name of the variable... this is NOT AT ALL what I am trying to do. I'm not TRYING to be difficult here, seriously. I also appreciate you TRYING to send me in the direction of the answer I am looking for. The TITLE of the question may seem similar, but the meat of the question directly below is very different. (Also my current reputation will not allow me to comment on that question to ask for specifics! I believed posting a question under the guise of an answer is against the rules.) – Ronk Feb 08 '16 at 06:39
  • Again THANK YOU for trying to help but: "I want to create a log function where I can insert variable names like this:" Does not cover: "Can a method or function of a variable/array/object contain the name of the object/array/variable itself ?" I will try altering the title of my question slightly. – Ronk Feb 08 '16 at 06:44
  • I've reopened it, but it seems to me you want the name of the variable that references an array, which is a very common question. Even your rejected solution fits one of the answers here and one for the duplicate where the question is in a different context but trying to d the same thing. Anyhow, fill 'yer boots. ;-) – RobG Feb 08 '16 at 07:31
  • Thank you Rob. I realize now that it DOES NOT EXIST. So I will use my workaround. I realize they were looking for the same thing, but for different reasons, in other situations, which is why I was holding firm to my "unique" assertion. IMHO It's like me saying: "How do I use a light-saber to open a can of soup without boiling away the contents" and someone else saying: "How do I use a light-saber to cut through a snowdrift to find my house, without burning the house down." So: "A light-saber does not exist, get real" is the answer to BOTH questions, but the questions themselves are different. – Ronk Feb 08 '16 at 07:41

3 Answers3

1

The easiest way to do this is use an object to store all the arrays and a variable for the array property you want

x = 3;
var arrayToUseName = "FirstNames", arrayToUse,
data = {
  FirstNames: ["John", "Paul", "George", "Ringo"],
  LastNames: ["Smith", "Jonson", "Jones", "Doe"],
  SemiRandomNumbers: [3, 6, 7, 4, 2, 1, 0, 9]
}

arrayToUse = data[arrayToUseName]

then to change array

arrayToUseName="LastNames";
arrayToUse = data[arrayToUseName];

Now you always have access to arrayToUseName.


To do the same with your variables they would have to be in global namespace and use

window[arrayToUseName];

But putting things in global namespace can create collisions you prefer to avoid

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Yes they ARE global. I realize the danger of collisions here, but I have 30+ functions calling these arrays. I did not want to add arguments each time the functions were called, as some of them overlap. I may change that in the future as this "project" grows. But for now, it is just easier. – Ronk Feb 08 '16 at 06:16
  • 1
    so you put them in one object in global...is not a big deal to access the object. Also may want to look into using browser webStorage also – charlietfl Feb 08 '16 at 06:18
  • I am using LocalStorage, thanks for the tip though! I could have used it a few years ago! Cookies were not storing enough info. And I store user's local info on how one or more of their database objects interact with each other. – Ronk Feb 08 '16 at 06:22
0

Yes, it's simple.

You have:

alert( "The Array used Was: " + ArrayToUse + "." )

which will give the contents of ArrayToUse. If you want to print the variable name, you need to surround it in double quotes, such as:

alert( "The Array used Was: " + "ArrayToUse" + "." )

which will use the variable name instead.

lk81
  • 54
  • 4
  • 1
    I think he wanted the variable name of the chosen variable to be dynamic, i.e. no matter what array he chose, it would print out its name. – Catalyst Feb 08 '16 at 05:05
  • 1
    Wrapping quotes around a variable name just makes it a literal string and will print exactly what you see. There is no relationship between that variable name and the string – charlietfl Feb 08 '16 at 05:14
  • 1
    @charlietfl There is a relationship. It works for any variable name. For example, `"test_name"` prints out the name of the variable called `test_name`. Likewise, `"foo"` prints out the name of the variable called `foo`. For any variable `x`, just throw double quotes around it and you have its name. – lk81 Feb 08 '16 at 05:43
  • 2
    will bite my words when you create demo that exhibits this behavior with the code you used. There is nothing in ES5 javascript that will change anything within a string by simply wrapping quotes round it – charlietfl Feb 08 '16 at 05:45
  • *"For any variable x, just throw double quotes around it and you have its name"* - this only works in the sense that you have hardcoded the variable name as a string. Which means you have to know the variable name at *design* time. In the OP's case *at runtime* we only have a reference to an array, we don't have a link to the original *other* variable that references the same array. – nnnnnn Feb 08 '16 at 05:50
  • @charlietfl Maybe you can give an example where it fails? I've already given examples of it working. – lk81 Feb 08 '16 at 05:51
  • 1
    @nnnnnn Yes. It's impossible to know even how *many* other variable names reference the same array, much less what they're called. The double quote method only gives you the name of the variable you're currently using to reference the array. And you must already know that at design time, otherwise you couldn't use the variable in the first place. – lk81 Feb 08 '16 at 05:54
  • Catalyst, YES I want it to be dynamic, VERY dynamic. I was going to use charlietfl's answer above and have EVERYTHING (ALL arrays) inside one giant object, but as I mentioned, some of my arrays are just way too big, I only want to invoke them when absolutely necessary, to avoid hogging bandwidth from server to client, and to save clients memory usage. This is being used on phones and tablets with slow and/or expensive data plans. – Ronk Feb 08 '16 at 05:58
  • @Ronk size of your arrays is irrelevant. Object case I showed you won't care and properties can be extended or removed at any time – charlietfl Feb 08 '16 at 06:00
  • I want the program to produce the behavior on it's own, not by the user having the ability to add quotes and/or properties while editing the server code each time he/she chooses. – Ronk Feb 08 '16 at 06:01
  • Tell the user who only wanted to download 25k that size is irrelevant when he just enabled a 5GB database and streamed it to his iPhone while roaming. ;-p – Ronk Feb 08 '16 at 06:04
  • 1
    @Ronk none of that has anything to do with how you structure the data to be able to access it with your code .... up to other parts of the app to sort out delivery and size. The question was how to be able to access it and switch arrays easily – charlietfl Feb 08 '16 at 06:06
  • Ik81- can you give an example of what you mean? I'm not sure I understood the syntax. – Ronk Feb 08 '16 at 06:09
-2

Whenever you reference something (for example to print it), in JavaScript, it can be done either by enclosing it in "quotes" or without.

Whenever you enclose in quotes, JavaScript renders it as it is, meaning it will consider it as a literal string.

If you don't print in quotes, it considers it as variable, and use the value of the variable.

"FirstNames"   - String
FirstName      - JavaScript variable
John Hascall
  • 9,176
  • 6
  • 48
  • 72