如何在javascript中使用复杂的关联数组?
我需要这张表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,JavaScript 有“数组”和“对象”。我认为“关联数组”指的是一个 JavaScript 对象,使用非负整数以外的键。
您可以使用如下语法创建 JavaScript 对象文字:
然后,您可以像这样访问属性:
要循环访问值,您可以执行以下操作:
编辑:编写完上述内容后,如果您 < em>仅想要根据级别和数字查找版本,您可能应该反转哈希。不要循环和检查版本,而是直接索引它们并在恒定时间内查找:
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:
You would then access the properties like so:
To loop through the values, you could do this:
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: