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

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

问题描述

我希望能够让自己的英语和我想创建清晰。我第一次开始与我想要的东西。

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

我想打一个IBANcalculator能够产生1-N IBANnumbers也验证给定IBANnumber。 IBANnumbers在许多国家的支付和我想使可以用来生成数字测试目的的工具使用。

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.

维基(荷兰网站)我发现国家名单和他们的定义方式IBANnumber。我想要做什么它做一种包含所有与他们的姓名,code,有IBANlength的bankingformat和帐户格式国家的数组。

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.

我做媒体链接的表像这样保存的信息(此表没有使用anyware,但它是我给你展示一下我在心里对结构是如何的好方法):

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

(请注意,我用的Uni code转义为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.)

使用对象的文字符号来创建单个对象和整个地图。在整体地图,还有每个国家的财产,与物业键为全国code和属性值是一个对象,提供从表中的信息。

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.

您然后可以使用它的国家code这样查找一个国家的信息在地图上:

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

或者,如果你在另一个变量全国code:

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"

显然,如果你preFER比国家code以外的东西来查找,只需调整相应的东西。

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

在以信息做这个我没有什么控制权,我通常把preFIX的关键,以避免运行到问题与内置的一个对象的属性(虽然我不认为有太多的冲突冲突的可能性在这里)。如果您使用了CCpreFIX,例如,事情是这样的:

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对象是地图,你甚至可以按国家code结合数字索引和索引。这里是一个例子:

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

这就是那些prefixes变得非常重要,因为阵列具有比普通的对象更多的属性。

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

按国家code中的查找是不变的:

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

国家code查找然后使用 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"

你就在那儿,一堆选项:

So there you are, a bunch of options:


  • 的数组(查找由数字指标,而不是由国家code,这是由其他答案覆盖,或只保留交叉索引循环脱以上)

  • 图(查找由国家code,而不是数字索引)

  • 具有附加属性的数组(通过数字索引查找的的国家code)

  • 上有一个单独的 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天全站免登陆