javascript多维数组?

Meg*_*020 8 javascript arrays

我希望我能用英语和我想创造的东西清楚自己.我首先从我想要的开始.

我想制作一个IBAN计算器,它可以生成1-n IBANnumbers并验证给定的IBANnumber.IBANnumbers在许多国家/地区用于支付,我想要制作的工具可用于生成用于测试目的的数字.

维基百科(荷兰网站)上,我找到了一个列表,其中列出了国家及其定义IBAN编号的方式.我想做的是制作一种数组,其中包含所有国家/地区的名称,代码,IBANlength,银行格式和帐户格式.

该数组需要用于:

  1. 生成选择列表(用于选择国家/地区)
  2. 用于检查生成数字的部分
  3. 用于检查部分验证号码

我不知道阵列是否是最好的方法,但这是迄今为止我所拥有的最多的知识.

我已经制作了一个这样的表格来保存信息(这个表格没有使用任何软件,但这是一个很好的方式让我向你展示我对结构的看法):

<table>
 <tr>
  <td>countryname</td>
  <td>country code</td>
  <td>valid IBAN length</td>
  <td>Bank/Branch Code (check1, bank, branch)</td>
  <td>Account Number (check2, number, check3)</td>
 <tr>
 <tr>
  <td>Andorra</td>
  <td>AD</td>
  <td>24</td>
  <td>0  4n 4n</td>
  <td>0  12   0 </td>
 <tr>
 <tr>
  <td>België</td>
  <td>BE</td>
  <td>16</td>
  <td>0  3n 0 </td>
  <td>0   7n  2n</td>
 <tr>
 <tr>
  <td>Bosnië-Herzegovina</td>
  <td>BA</td>
  <td>20</td>
  <td>0  3n 3n</td>
  <td>0   8n  2n</td>
 <tr>
</table>
Run Code Online (Sandbox Code Playgroud)

还有很多.

T.J*_*der 40

主要答案

我根本不会使用"数组".JavaScript对象是映射(有时称为"关联数组",但让我们使用"map"来避免与数字索引数组混淆),因此您可以非常轻松地使用普通对象执行此操作:

var IBANInfo = {
    "AD": {
        countryCode: "AD",
        countryName: "Andorra",
        length: 24,
        bankBranchCode: "0  4n 4n",
        accountNum: "0  12   0"
    },
    "BE": {
        countryCode: "BE",
        countryName: "Belgi\u00EB",
        length: 16,
        bankBranchCode: "0  3n 0",
        accountNum: "0  7n   2n"
    },
    "BA": {
        countryCode: "BA",
        countryName: "Bosni\u00EB-Herzegovina",
        length: 20,
        bankBranchCode: "0  3n 3n",
        accountNum: "0   8n  2n"
    }
};
Run Code Online (Sandbox Code Playgroud)

(请注意,我已经将'e'用于带有变音符号的'e'的Unicode转义;可能是最好的,尽管如果你对你的编码很小心,你应该没问题.)

它使用对象文字表示法来创建单个对象和整个地图.在整个地图中,每个国家/地区都有一个属性,其中属性键是国家/地区代码,属性值是提供表中信息的对象.

然后,您可以使用以下国家/地区代码在地图中查找国家/地区的信息:

var countryInfo = IBANInfo["AD"]; // <= Example for Andorra
Run Code Online (Sandbox Code Playgroud)

或者,如果您在另一个变量中有国家/地区代码:

var countryCode = "AD"; // <= Example for Andorra
var countryInfo = IBANInfo[countryCode];
alert("Country name: " + countryInfo.countryName); // <= Alerts "Country name: Andorra"
Run Code Online (Sandbox Code Playgroud)

显然,如果您希望通过国家/地区代码以外的其他方式查找,请相应地调整内容.

在偏执狂的键上放一个前缀

使用我无法控制的信息执行此操作时,我通常会在键上放置一个前缀,以避免遇到与对象的内置属性冲突的问题(尽管我认为此处不存在冲突的可能性).例如,如果使用"cc"前缀,事情将如下所示:

地图:

var IBANInfo = {
    "ccAD": {
        countryCode: "AD",
        countryName: "Andorra",
        length: 24,
        bankBranchCode: "0  4n 4n",
        accountNum: "0  12   0"
    },
    "ccBE": {
        countryCode: "BE",
        countryName: "Belgi\u00EB",
        length: 16,
        bankBranchCode: "0  3n 0",
        accountNum: "0  7n   2n"
    },
    "ccBA": {
        countryCode: "BA",
        countryName: "Bosni\u00EB-Herzegovina",
        length: 20,
        bankBranchCode: "0  3n 3n",
        accountNum: "0   8n  2n"
    }
};
Run Code Online (Sandbox Code Playgroud)

查找:

var countryCode = "AD";                          // <= Example for Andorra
var countryInfo = IBANInfo["cc" + countryCode];  // <= Note we add the prefix on lookup
alert("Country name: " + countryInfo.countryName); // <= Alerts "Country name: Andorra"
Run Code Online (Sandbox Code Playgroud)

循环通过地图中的键

如果您需要(出于任何原因)遍历所有这些,因为它不是数组,您不能使用数字索引.幸运的是,这正是JavaScript for..in循环的用途:它查看对象属性的名称(键):

var key;
for (key in IBANInfo) {
    if (IBANInfo.hasOwnProperty(key)) {
        // ...use key here, it'll be "ccAD" for Andorra, etc...
    }
 }
Run Code Online (Sandbox Code Playgroud)

(您可以使用hasOwnProperty该对象已在其上设置的属性来区分直接与那些从它的原型得到.如果你不熟悉JavaScript的原型继承,不用太担心,只是一定要使用像一个循环以上.)

两全其美

由于JavaScript数组是对象,并且所有JavaScript对象都是映射,因此您甚至可以按国家/地区代码组合数字索引和索引.这是一个例子:

地图:

// First, build the array
var IBANInfo = [
    {
        countryCode: "AD",
        countryName: "Andorra",
        length: 24,
        bankBranchCode: "0  4n 4n",
        accountNum: "0  12   0"
    },
    {
        countryCode: "BE",
        countryName: "Belgi\u00EB",
        length: 16,
        bankBranchCode: "0  3n 0",
        accountNum: "0  7n   2n"
    },
    {
        countryCode: "BA",
        countryName: "Bosni\u00EB-Herzegovina",
        length: 20,
        bankBranchCode: "0  3n 3n",
        accountNum: "0   8n  2n"
    }
];

// Now, cross-index it
var index, entry;
for (index = 0; index < IBANInfo.length; ++index)
{
    // Get the entry at this numeric index
    entry = IBANInfo[index];

    // Create the country code lookup for it
    IBANInfo["cc" + entry.countryCode] = entry;
}
Run Code Online (Sandbox Code Playgroud)

这是前缀变得非常重要的地方,因为数组比普通对象具有更多属性.

按国家/地区代码查找不变:

var countryCode = "AD";
var countryInfo = IBANInfo["cc" + countryCode];    // <= Country code lookup
alert("Country name: " + countryInfo.countryName); // <= Alerts "Country name: Andorra"
Run Code Online (Sandbox Code Playgroud)

但是现在如果(出于某种原因)你需要使用数字索引,你也可以这样做:

var countryInfo = IBANInfo[0];                      // <= Numeric lookup
alert("Country name: " + countryInfo.countryName); // <= Also alerts "Country name: Andorra"
Run Code Online (Sandbox Code Playgroud)

事后的交叉索引作为aboev最好只适用于像IBAN地图这样的静态内容.如果您要在程序中添加或删除条目,我可能会从中创建一个可重用的对象.

如果我需要数字和键的东西,我通常会通过使map方面成为数组的属性而不是直接使用数组来分离一些东西.在我们初始化数组之后,这只需要对我们的循环进行一些小的更改即可创建地图:

地图:

// First, build the array
var IBANInfo = [
    /* ...same as before, omitted for space... */
];

// Now, cross-index it
var index, entry;
IBANInfo.byCC = {}; // A new plain object to be our map
for (index = 0; index < IBANInfo.length; ++index)
{
    // Get the entry at this numeric index
    entry = IBANInfo[index];

    // Create the country code lookup for it
    IBANInfo.byCC["cc" + entry.countryCode] = entry;
}
Run Code Online (Sandbox Code Playgroud)

然后,国家/地区代码查找使用该byCC属性:

var countryCode = "AD";
var countryInfo = IBANInfo.byCC["cc" + countryCode]; // <= Country code lookup
   // The change is here:-^^^^^
alert("Country name: " + countryInfo.countryName);  // <= Alerts "Country name: Andorra"
Run Code Online (Sandbox Code Playgroud)

所以,你有很多选择:

  • 一个数组(按数字索引查找,而不是按国家/地区代码查找;这可以通过其他答案覆盖,或者只是将交叉索引循环保留在上面)
  • 地图(按国家/地区代码查找,而不是按数字索引查找)
  • 具有其他属性的数组(通过数字索引国家/地区代码查找)
  • 一个带有独立byCC属性的数组,只是为了让我们都清醒

快乐的编码.