javascript多维数组? [英] javascript multidimensional array?

查看:50
本文介绍了javascript多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I hope I can make myself clear in English and in what I want to create. I first start with what I want.

我想制作一个 IBAN 计算器,它可以生成 1-n 个 IBAN 编号并验证给定的 IBAN 编号.许多国家/地区都使用 IBAN 号码进行支付,我想制作的工具可用于生成号码以进行测试.

I want to make a IBANcalculator that can generate 1-n IBANnumbers and also validate a given IBANnumber. IBANnumbers are used in many countries for payment and the tool I want to make can be used to generate the numbers for testing purpose.

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

On wikipedia (Dutch site) I found a list with countries and their way of defining the IBANnumber. What I want to do it to make a kind of an array that holds all the countries with their name, the code, there IBANlength, the bankingformat and the account format.

数组需要用于:

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

我不知道数组是否是最好的方法,但这是我目前掌握的最多的知识.

I don't know if an array is the best way, but that's so far the most knowledge I have.

我已经制作了一个这样的表格来保存信息(这个表格在任何情况下都没有使用,但它是我向您展示我对结构的看法的好方法):

I allready made a table like this that holds the info (this table isn't used anyware, but it was a good way for me to show you what I have in mind about how the structure is):

<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 对象是映射(有时称为关联数组",但让我们使用映射"来避免与数字索引数组混淆),因此您可以很容易地对普通对象执行此操作:

The main answer

I wouldn't use an "array" for this at all. JavaScript objects are maps (sometimes called "associative arrays," but let's use "map" to avoid confusion with numerically-indexed arrays), so you can do this with plain objects quite easily:

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

(请注意,我使用了 Unicode 转义符来表示 'e' 上面的变音符号;可能是最好的,尽管如果您对编码很小心,应该没问题.)

(Note that I've used the Unicode escape for the 'e' with the umlaut above it; probably for the best, although if you're careful with your encodings you should be fine.)

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

That uses object literal notation to create the individual objects and the overall map. In the overall map, there is a property for each country, with the property key being the country code and the property value being an object providing the information from your table.

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

You can then look up a country's information in the map using its country code like this:

var countryInfo = IBANInfo["AD"]; // <= Example for Andorra

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

Or if you have the country code in another variable:

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

显然,如果您更喜欢按国家/地区代码以外的其他内容查找,只需相应地调整即可.

Obviously if you prefer to look up by something other than country code, just adjust things accordingly.

当使用我几乎无法控制的信息执行此操作时,我通常在键上添加前缀以避免遇到与对象的内置属性冲突的问题(尽管我认为冲突的可能性不大这里).例如,如果您使用了cc"前缀,事情将如下所示:

When doing this with information I have little control over, I usually put a prefix on the key to avoid running into issues with conflicts with the built-in properties of an object (although I don't think there's much likelihood of conflict here). If you used a "cc" prefix, for instance, things would look like this:

地图:

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 循环的用途:它查看对象属性的名称(键):

Looping through the keys in the map

If you need (for any reason) to loop through all of these, since it's not an array you can't use a numeric index. Fortunately, this is exactly what the JavaScript for..in loop is for: It looks through the names (keys) of the object's properties:

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

(您使用 hasOwnProperty 来区分对象直接设置的属性与从其原型获得的属性.如果您不熟悉 JavaScript 的原型继承,不用太担心,一定要使用上面这样的循环.)

(You use hasOwnProperty to differentiate between properties the object has set on it directly vs. those it gets from its prototype. If you're not familiar with JavaScript's prototypical inheritance, don't worry too much, just be sure to use a loop like the above.)

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

Since JavaScript arrays are objects, and all JavaScript objects are maps, you can even combine numeric indexing and indexing by country code. Here's an example of that:

地图:

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

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

This is where those prefixes become very important, because arrays have more properties than plain objects.

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

The lookup by country code is unchanged:

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

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

But now if (for some reason) you need to use a numeric index, you can also do that:

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

事后交叉索引,因为 aboev 仅适用于静态事物,例如您的 IBAN 地图.如果你打算在你的程序中添加或删除条目,我可能会用它来制作一个可重用的对象.

Cross-indexing after the fact as aboev is best only for static things like your IBAN map. If you were going to be adding or removing entries from this as part of your program, I'd probably make a reusable object out of it instead.

如果我需要数字和键的东西,我通常通过将地图方面作为数组的属性而不是直接使用数组来将事物分开.在我们初始化数组之后,只需要对创建映射的循环稍作改动即可:

If I need things both numerically and by a key, I usually separate things out a bit by making the map aspects a property of the array rather than using the array directly. That requires only a small change to our loop that creates the map after we've initialized the array:

地图:

// 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 属性:

Country code lookups then use the byCC property:

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"

所以你有很多选择:

  • 一个数组(按数字索引查找,而不是按国家/地区代码查找;其他答案涵盖了这一点,或者只是将交叉索引循环放在上面)
  • 一张地图(按国家代码查找,而不是按数字索引)
  • 具有附加属性的数组(通过数字索引国家/地区代码查找)
  • 一个带有单独的 byCC 属性的数组,只是为了让我们保持理智
  • An array (look up by numeric index, not by country code; this is covered by other answers, or just leave the cross-indexing loop off the above)
  • A map (look up by country code, not by numeric index)
  • An array with additional properties (look up via numeric index or country code)
  • An array with a separate byCC property on it, just to keep us all sane

快乐编码.

这篇关于javascript多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆