简体   繁体   English

使用变量引用数组

[英]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_'. 因此,问题在于“ vArrName”是一个值为“ arr_”的字符串,而不是对名称为“ arr_”的数组的引用。

I tried wrapping it with the eval() function, but eval() treats dashes as minus signs. 我尝试用eval()函数包装它,但是eval()将破折号视为减号。

One other note: I cannot use jquery for this effort. 另一个注意事项:我不能为此使用jquery。

Thank you 谢谢

You cannot generate a reference to a variable declared with var (except see below). 您无法生成对使用var声明的变量的引用(除非参见下文)。 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. 无法通过动态名称访问var变量的例外是浏览器中的全局变量。 Those variables all end up as properties of the window object. 这些变量最终都作为window对象的属性。

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};
  • An array in JavaScript is delimitated by [ and ] , not ( or ) . JavaScript中的数组由[]分隔,而不是()分隔。
  • A valid JavaScript variable name can't contain '-' 有效的JavaScript变量名称不能包含“-”
  • The length property of an array isn't a function 数组的length属性不是函数

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;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM