Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've seen a few other posts about this on Stack Overflow, but the answer always seems to be to create an object with key / value pairs. This doesn't seem to be what I need in my current situation. What I'm wanting to do: I have different arrays which could possibly contain a username. I want to check each array and see if the username is present as a value in them. If it is, I'd like a string representation of a name of the array variable. Example:

var array = ['apple', 'orange', 'grape'];
var array2 = ['apple', 'pear', 'plumb']

member_arrays = new Array();
// I'd like this block to be dynamic in that i don't have to specify the array name 
// in the inArray or member_arrays[member_arrays.length+1] (just loop through my arrays
// and add a string representation of the array name to the member_arrays array)
if ($.inArray( 'apple', array ) != -1)
  member_arrays[member_arrays.length] = 'array';
if ($.inArray( 'apple', array2) != -1)
  member_arrays[member_arrays.length] = 'array2';
// etc...
share|improve this question
1  
Add parentheses around your if block, and the code works as intended, using jQuery ($.inArray), eg: if ($.inArray('apple', array) != -1) member_arrays.push('array'); – Rob W Nov 2 '11 at 16:39
1  
Sorry I've been coding in Coffeescript lately and have been leaving off the parenthesis. The code above WILL work, but I'm wanting to get away from explicitly naming each array in the if statements. – David Savage Nov 2 '11 at 16:40

2 Answers

up vote 4 down vote accepted

You cannot do that in JavaScript. That's why people suggest using an object and keeping your "variables" as properties in the object.

By the way, when you're appending to an array, you want just the length, not length + 1:

member_arrays[member_arrays.length] = 'array';

Arrays are zero-based, so the "next" slot is always the "length" value.

edit — well, there is a case where you can do that: when your variables are global. Any global variable named "x" can also be referred to as a property of "window" (for code in a web browser):

var x = 3;
alert(window['x']); // alerts "3"

Please avoid global variables for this purpose :-)

share|improve this answer
Good point about zero-based arrays....pointy. Well, that's discouraging, but I guess I could just make my arrays properties of an object to get around this, eh? – David Savage Nov 2 '11 at 16:42
Yes, you can do that and it works fine. The basic problem is that there's no way to make a "pointer" to a variable, and there's no way to refer to a local scope (that is, there's nothing like "window" for variables local to a function). – Pointy Nov 2 '11 at 16:43
So, to play devil's advocate to your edited option, one COULD do this: for (i in window) { if ($.inArray ( username, window[i] ) != -1) { member_arrays.push(i); }}, it's just bad practice? – David Savage Nov 2 '11 at 16:48
2  
Yes you could do that, or make an array of the string names of your variables and iterate over that, looking up the names in "window" as you go. Basically you're just taking advantage of the fact that "window" is already an object, and that's just how global variables work in JavaScript. (There are lots of properties on "window" of course so just iterating through all of them will cause you to run into a lot of weird things.) – Pointy Nov 2 '11 at 16:50
Ah, good reason not to do that then ;) Thanks for the help! – David Savage Nov 2 '11 at 16:50

I'm not sure why you'd want to do things this way, but putting that aside, here's an approach that approximates what you seem to be looking for. It does use an object, as others have recommended, but you end up with the "answer array" containing the names of the candidate arrays passing the test.

You do use jQuery in your sample, so I've done so as well. But you can also use the plain JavaScript .indexOf() the same way.

var candidates = {
  'array' : ['apple', 'orange', 'grape'],
  'array2' : ['apple', 'pear', 'plumb']
};
var member_arrays = [];

for( var test in candidates ){
  if( $.inArray( 'apple', candidates[test] ) !== -1 ){ // could use .indexOf()
    member_arrays.push( test ); // use push; no need to specify an index
  }
}

console.log( member_arrays ); // -> ["array","array2"]
share|improve this answer
Thanks Ken, that's the approach I ended up taking. You mention you're not sure why I'd want to do it this way...can you offer an alternative way to find this info out that's better? – David Savage Nov 2 '11 at 17:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.