我希望我能用英语和我想创造的东西表达清楚。我首先从我想要的开始。
我想制作一个 IBAN 计算器,它可以生成 1-n 个 IBAN 号码并验证给定的 IBAN 号码。 IBAN 号码在许多国家/地区用于支付,我想制作的工具可用于生成用于测试目的的号码。
关于 wikipedia (荷兰站点)我找到了一个列表,其中包含国家及其定义 IBAN 号码的方式。我想做的是制作一种数组,其中包含所有国家/地区的名称、代码、IBAN 长度、银行格式和帐户格式。
数组需要用于:
- 生成一个选择列表(用于选择国家)
- 用于检查生成数字的部分
- 用于验证数字的校验部分
我不知道数组是否是最好的方式,但这是迄今为止我掌握的最多的知识。
我已经制作了一个这样的表格来保存信息(这个表格没有被使用,但这是一个很好的方式来向你展示我对结构的想法):
<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>
还有更多。
最佳答案
主要答案
我根本不会为此使用“数组”。 JavaScript 对象是映射(有时称为“关联数组”,但我们使用“映射”以避免与数字索引数组混淆),因此您可以很容易地使用普通对象执行此操作:
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"
}
};
(请注意,我对上面带有变音符号的“e”使用了 Unicode 转义符;这可能是最好的,但如果您小心处理编码,应该没问题。)
它使用对象字面量符号来创建单个对象和整个 map 。在整个 map 中,每个国家/地区都有一个属性,属性键是国家/地区代码,属性值是提供表中信息的对象。
然后您可以使用国家代码在 map 中查找国家信息,如下所示:
var countryInfo = IBANInfo["AD"]; // <= Example for Andorra
或者如果您在另一个变量中有国家代码:
var countryCode = "AD"; // <= Example for Andorra
var countryInfo = IBANInfo[countryCode];
alert("Country name: " + countryInfo.countryName); // <= Alerts "Country name: Andorra"
显然,如果您更喜欢通过国家代码以外的其他方式进行查找,只需相应地进行调整即可。
出于偏执狂在键上加上前缀
当使用我几乎无法控制的信息执行此操作时,我通常会在键上加上前缀以避免遇到与对象的内置属性发生冲突的问题(尽管我认为发生冲突的可能性不大这里)。例如,如果您使用“cc”前缀,事情将如下所示:
map :
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"
}
};
查找:
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"
遍历映射中的键
如果您需要(出于任何原因)遍历所有这些,因为它不是数组,您不能使用数字索引。幸运的是,这正是 JavaScript for..in
循环的用途:它查看对象属性的名称(键):
var key;
for (key in IBANInfo) {
if (IBANInfo.hasOwnProperty(key)) {
// ...use key here, it'll be "ccAD" for Andorra, etc...
}
}
(您使用 hasOwnProperty
来区分对象直接设置的属性与它从其原型(prototype)获得的属性。如果您不熟悉 JavaScript 的原型(prototype)继承,不用太担心,一定要像上面那样使用循环即可。)
两全其美
由于 JavaScript 数组是对象,而所有 JavaScript 对象都是 map ,您甚至可以结合使用数字索引和国家代码索引。这是一个例子:
map :
// 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;
}
这就是这些前缀变得非常重要的地方,因为数组比普通对象具有更多的属性。
按国家代码查找不变:
var countryCode = "AD";
var countryInfo = IBANInfo["cc" + countryCode]; // <= Country code lookup
alert("Country name: " + countryInfo.countryName); // <= Alerts "Country name: Andorra"
但是现在如果(出于某种原因)你需要使用数字索引,你也可以这样做:
var countryInfo = IBANInfo[0]; // <= Numeric lookup
alert("Country name: " + countryInfo.countryName); // <= Also alerts "Country name: Andorra"
如前所述,事后交叉索引最适合静态事物,例如您的 IBAN map 。如果您打算在程序中添加或删除条目,我可能会改为使用它制作一个可重用的对象。
如果我同时需要数字和键值的东西,我通常会通过将 map 方面作为数组的属性而不是直接使用数组来稍微分开一些东西。这只需要对我们在初始化数组后创建 map 的循环做一个小改动:
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;
}
国家代码查找然后使用 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"
所以你有很多选择:
- 一个数组(按数字索引查找,而不是按国家/地区代码查找;这在其他答案中有所介绍,或者只是将交叉索引循环从上面去掉)
- 一张 map (按国家代码查找,而不是按数字索引查找)
- 具有附加属性的数组(通过数字索引或国家代码查找)
- 一个数组,上面有一个单独的
byCC
属性,只是为了让我们保持清醒
快乐编码。
关于javascript多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2529865/