Refer to an array using a variable

I am writing a page that collects serial numbers for parts installed in an assembly. I want to validate the user input on the client-side, if I can.

So if I have multiple dense arrarys, how can I refer to them using a varaiable? For instance, say I have three densely packed arrays who's names represent part numbers, and who's values represent serial numbers (that have been consumed in other assemblies).

arr_PN-123-ABC = ('SN0123','SN0124','SN0125')
arr_PN-456-DEF = ('SN00333','SN00334','SN00335')
arr_PN-789-GHI = ('SN-0001','SN-0002','SN-0003','SN-0004')

function fcnValidateSN(_givenPN, _givenSN) {

  //make sure the given values are not null or empty...

  //derive the array of serial numbers that coorsponds to the given part number...
  var vArrName = "arr_" + vGivenPN;

  //loop thru the array of serial numbers to determine if the given sn was already used...
  for(var x=0; x < vArrName.length(); x++) {
    if(vArrName[x]==_givenSN) {
      alert("Serial number '" + _givenSN + "' was already used in another assembly.");
      theForm.txtPN.focus();
      return;
    }


  } //end 'for' loop

} //end fcnValidateSN()

So the problem is that 'vArrName' is a string with a value of 'arr_' instead of a refernece to an array who's name is 'arr_'.

I tried wrapping it with the eval() function, but eval() treats dashes as minus signs.

One other note: I cannot use jquery for this effort.

Thank you

Upvotes: 0

Views: 77

Answers (3)

Alcides Queiroz
Alcides Queiroz

Reputation: 9576

  • An array in JavaScript is delimitated by [ and ], not ( or ).
  • A valid JavaScript variable name can't contain '-'
  • The length property of an array isn't a function

Well, I've done some (actually, a lot of) adjustments in your code, but I think this is what you need:

var serialGroups = {
  PN_123_ABC: ['SN0123','SN0124','SN0125'],
  PN_456_DEF: ['SN00333','SN00334','SN00335'],
  PN_789_GHI: ['SN-0001','SN-0002','SN-0003','SN-0004']
};

function validateSerial(groupName, sn) {
  var serials = serialGroups[groupName];

  for(var i=0; i < serials.length; i++){
    if(serials[i] == sn) {
      alert("Serial number '" + sn + "' was already used in another assembly.");
      //Do whatever you want here
      return;
    }
  }
}

Upvotes: 0

Dan Smolinske
Dan Smolinske

Reputation: 319

Use a single object that has the arrays as elements:

var arr_PN = {
    '123-ABC': ('SN0123','SN0124','SN0125'),
    '456-DEF': ('SN00333','SN00334','SN00335'),
    '789-GHI': ('SN-0001','SN-0002','SN-0003','SN-0004')
}

And then reference using:

var vArrName = arr_PN->{vGivenPN};

Upvotes: 0

Pointy
Pointy

Reputation: 413720

You cannot generate a reference to a variable declared with var (except see below). You can use dynamic property names to refer to properties of objects, so:

var arrays = {
  "arr_PN-123-ABC": ['SN0123','SN0124','SN0125'],
  "arr_PN-456-DEF": ['SN00333','SN00334','SN00335'],
  // ...
};

Then:

console.log( arrays["arr_PN-" + num + "ABC"][0] ); // SN0123

Note that you cannot use "-" in a variable name, but you can use it in an object property name.

The exception to not being able to access var variables by dynamic name is made for global variables in a browser. Those variables all end up as properties of the window object.

Upvotes: 1

Related Questions