如何在javascript中使用复杂的关联数组?

发布于 2024-10-09 04:01:34 字数 263 浏览 0 评论 0原文

我需要这张表 http://code.google.com/apis /chart/docs/gallery/qr_codes.html#details 在我的程序中,我什至不确定关联数组是否是正确的方法。

给定类型(数字/字母数字)、字符数和 EC(纠错)级别,我想要一个函数来返回版本(第一列)。

I need this table http://code.google.com/apis/chart/docs/gallery/qr_codes.html#details in my program and I'm not even sure if an associative array is the way to go.

Given the type (numeric/alphanumeric), number of characters and EC (error correction) level, I want a function to return the version (first column).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开柳相依 2024-10-16 04:01:34

首先,JavaScript 有“数组”和“对象”。我认为“关联数组”指的是一个 JavaScript 对象,使用非负整数以外的键。

您可以使用如下语法创建 JavaScript 对象文字:

var versions = {
  "1" : {
    rowcols : [21,21],
    charsByECLevel : {
      L : {
        digits:41,
        alpha:25
      },
      M : {
        digits:34,
        alpha:20
      }
    }
  },
  "2" : {
    rowcols : [25,25],
    charsByECLevel : {
      L : {
        digits:77,
        alpha:47
      },
      M : {
        digits:63,
        alpha:48
      }
    }
  }
};

然后,您可以像这样访问属性:

console.log( versions[1].charsByECLevel.L.digits );
// 41

要循环访问值,您可以执行以下操作:

function findVersion( versions, level, digits ){
  for (var versionNumber in versions){
    if (versions.hasOwnProperty(versionNumber)){
      if (versions[versionNumber].charsByECLevel[level].digits == digits){
        return versionNumber;
      }
    }
  }
}

findVersion( versions, "L", 77 );
// returns "2"

编辑:编写完上述内容后,如果您 < em>仅想要根据级别和数字查找版本,您可能应该反转哈希。不要循环和检查版本,而是直接索引它们并在恒定时间内查找:

var versionByLevelAndDigits = {
  L : {
     41 : 1,
     77 : 2,
    127 : 3
  },
  M : {
     34 : 1,
     63 : 2,
    101 : 3
  }
};

var version = versionByLevelAndDigits["L"][77];
// 2

First, JavaScript has "Arrays" and "Objects". By 'associative array' I assume you mean a JavaScript Object, using keys other than non-negative integers.

You can create JavaScript object literals using syntax such as the following:

var versions = {
  "1" : {
    rowcols : [21,21],
    charsByECLevel : {
      L : {
        digits:41,
        alpha:25
      },
      M : {
        digits:34,
        alpha:20
      }
    }
  },
  "2" : {
    rowcols : [25,25],
    charsByECLevel : {
      L : {
        digits:77,
        alpha:47
      },
      M : {
        digits:63,
        alpha:48
      }
    }
  }
};

You would then access the properties like so:

console.log( versions[1].charsByECLevel.L.digits );
// 41

To loop through the values, you could do this:

function findVersion( versions, level, digits ){
  for (var versionNumber in versions){
    if (versions.hasOwnProperty(versionNumber)){
      if (versions[versionNumber].charsByECLevel[level].digits == digits){
        return versionNumber;
      }
    }
  }
}

findVersion( versions, "L", 77 );
// returns "2"

Edit: Having written the above, if you only want to look up versions based on level and digits, you should probably reverse the hash. Instead of looping through and checking the versions, index them directly and look it up in constant time:

var versionByLevelAndDigits = {
  L : {
     41 : 1,
     77 : 2,
    127 : 3
  },
  M : {
     34 : 1,
     63 : 2,
    101 : 3
  }
};

var version = versionByLevelAndDigits["L"][77];
// 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文