没有 toString 等的关联数组

发布于 2024-07-09 14:31:18 字数 506 浏览 8 评论 0原文

我想创建一个关联数组:

var aa = {} // Equivalent to Object(), new Object(), etc...

并且我想确保我访问的任何键都是数字:

aa['hey'] = 4.3;
aa['btar'] = 43.1;

我知道 JavaScript 没有打字功能,所以我无法自动检查这一点,但我可以确保在我的我自己的代码,我只将字符串分配给这个aa

现在我正在从用户那里获取密钥。 我想显示该键的值。 但是,如果用户给我类似“toString”的东西,用户会返回一个函数,而不是一个 int! 有没有办法确保用户给我的任何字符串都是我定义的?

唯一的解决方案是像下面这样吗?

delete aa['toString'];
delete aa['hasOwnProperty'];

ETC...

I want to create an associative array:

var aa = {} // Equivalent to Object(), new Object(), etc...

And I want to be sure that any key I access is going to be a number:

aa['hey'] = 4.3;
aa['btar'] = 43.1;

I know JavaScript doesn't have typing, so I can't automatically check this, but I can ensure in my own code that I only assign strings to this aa.

Now I'm taking keys from the user. I want to display the value for that key. However, if the user gives me something like "toString", the user gets back a function, not an int! Is there a way to make sure any string the user gives me is only something I define?

Is the only solution something like the following?

delete aa['toString'];
delete aa['hasOwnProperty'];

etc...

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

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

发布评论

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

评论(3

念﹏祤嫣 2024-07-16 14:31:18

这可能对您有用:

function getValue(id){
  return (!isNaN(aa[id])) ? aa[id] : undefined;
}

或者,我推荐这个通用解决方案:

function getValue(hash,key) {
    return Object.prototype.hasOwnProperty.call(hash,key) ? hash[key] : undefined;
}

请注意以下事项:键将在内部转换为字符串,因为键实际上是属性的名称。

var test = {
  2: "Defined as numeric",
  "2": "Defined as string"
}

alert(test[2]); // Alerts "Defined as string"

如果尝试使用对象:

var test = {}, test2 = {};
test[test2] = "message"; // Using an object as a key.

alert(test[test2]); // Alerts "message". It looks like it works...

alert(test[test2.toString()]);
// If it really was an object this would not have worked,
// but it also alerts "message".

现在您知道它始终是一个字符串,让我们使用它:

var test = {};

var test2 = {
    toString:function(){return "some_unique_value";}
    // Note that the attribute name (toString) don't need quotes.
}

test[test2] = "message";
alert(test["some_unique_value"]); // Alerts "message".

This may work for you:

function getValue(id){
  return (!isNaN(aa[id])) ? aa[id] : undefined;
}

Alternatively, I recommend this generic solution:

function getValue(hash,key) {
    return Object.prototype.hasOwnProperty.call(hash,key) ? hash[key] : undefined;
}

Note the following: The key will internally be converted to a string because the key is actually a name of an attribute.

var test = {
  2: "Defined as numeric",
  "2": "Defined as string"
}

alert(test[2]); // Alerts "Defined as string"

If trying to use an object:

var test = {}, test2 = {};
test[test2] = "message"; // Using an object as a key.

alert(test[test2]); // Alerts "message". It looks like it works...

alert(test[test2.toString()]);
// If it really was an object this would not have worked,
// but it also alerts "message".

Now that you know that it is always a string, let’s use it:

var test = {};

var test2 = {
    toString:function(){return "some_unique_value";}
    // Note that the attribute name (toString) don't need quotes.
}

test[test2] = "message";
alert(test["some_unique_value"]); // Alerts "message".
倾`听者〃 2024-07-16 14:31:18

一种可能性是使用 hasOwnProperty 来检查键是否是您显式添加到数组中的内容。 因此,

function findNumber(userEnteredKey) {
    return aa[userEnteredKey];
}

您可以说:

function findNumber(userEnteredKey) {
    if (Object.prototype.hasOwnProperty.call(aa, userEnteredKey))
        return aa[userEnteredKey];
}

或者,您可以使用 typeof 在返回之前检查任何内容是否为数字。 但我喜欢 hasOwnProperty 方法,因为它可以防止您返回任何您无意放入数组中的内容。

One possibility would be to use hasOwnProperty to check that the key is something you explicitly added to the array. So instead of:

function findNumber(userEnteredKey) {
    return aa[userEnteredKey];
}

you'd say:

function findNumber(userEnteredKey) {
    if (Object.prototype.hasOwnProperty.call(aa, userEnteredKey))
        return aa[userEnteredKey];
}

Alternately, you could use typeof to check that anything is a number before returning it. But I like the hasOwnProperty approach, because it'll keep you from returning anything that you didn't intentionally put in the array.

递刀给你 2024-07-16 14:31:18

当您创建新密钥时,请在其前面添加您自己的一些字符串常量:

var a = {};
var k = 'MYAPP.COLLECTIONFOO.KEY.';

function setkey(userstring)
{
  a[k + userstring] = 42;
}

function getkey(userstring)
{
  return a[k + userstring];
}

When you create a new key, prepend it with some string constant of your own:

var a = {};
var k = 'MYAPP.COLLECTIONFOO.KEY.';

function setkey(userstring)
{
  a[k + userstring] = 42;
}

function getkey(userstring)
{
  return a[k + userstring];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文