最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Associative array without toString, etc - Stack Overflow

matteradmin7PV0评论

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...

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...

Share Improve this question edited Aug 13, 2020 at 12:17 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Dec 15, 2008 at 3:58 ClaudiuClaudiu 230k173 gold badges503 silver badges698 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

This may work for you:

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

Alternatively, I remend 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".

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.

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far