检查键是否在关联数组中定义 [英] Check if key is defined in associative array

查看:81
本文介绍了检查键是否在关联数组中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查是否在关联数组中定义了键?


var users = new array();

users [" joe" ;] =" Joe Blow";

用户[" john"] =" John Doe" ;;

用户[" jane"] =" Jane Doe"


函数isUser(userID)

{

if(?????)

{alert(" This is a valid ID"); }

其他

}


但是代替????

How can I check if a key is defined in an associative array?

var users = new array();
users["joe"] = "Joe Blow";
users["john"] = "John Doe";
users["jane"] = "Jane Doe";

function isUser (userID)
{
if (?????)
{ alert ("This is a valid ID"); }
else
}

But what goes in place of the ????

推荐答案

2004年11月17日星期三20:21:24 +0000(UTC),JGH< jo ****** @ nospam.tds.net> ;

写道:
On Wed, 17 Nov 2004 20:21:24 +0000 (UTC), JGH <jo******@nospam.tds.net>
wrote:
如何检查键是否在关联数组中定义?


为了消除任何误解,ECMAScript / Javascript中没有关联的

数组。你实际使用的是一个功能

的对象 - 它们在运行时添加属性的能力。

var users = new array();
users [" joe"] =" Joe Blow";
users [" john"] =" John Doe";
users [" jane"] =" Jane Doe" ;;


使用实际阵列是一种浪费。你没有用它来存储价值

序数,你只是使用它是一个对象的事实。


var users = new Object();





var users = {}; //大括号,而不是括号


如果列表是静态的,你可以这样写:


var users = {

joe:''Jow Blow'',

john:''John Doe'',

jane:''Jane Doe''

};


如果id包含非法字符,请将属性名称指定为

字符串:


''mike-winter'':''Michael Winter''

函数是用户(userID)
{
if(?????)
{alert("这是一个有效的ID); }

}

但是代替????
How can I check if a key is defined in an associative array?
Just to dispel any misconceptions, there''s no such thing as an associative
array in ECMAScript/Javascript. What you are actually using is a feature
of objects - their ability to have properties added at run-time.
var users = new array();
users["joe"] = "Joe Blow";
users["john"] = "John Doe";
users["jane"] = "Jane Doe";
Using an actual array is a waste. You aren''t using it to store values by
ordinal number, you''re just using the fact that it''s an object.

var users = new Object();

or

var users = {}; // Braces, not parentheses

If the list is static, you could write:

var users = {
joe : ''Jow Blow'',
john : ''John Doe'',
jane : ''Jane Doe''
};

If an id contains an illegal character, specify the property name as a
string:

''mike-winter'' : ''Michael Winter''
function isUser (userID)
{
if (?????)
{ alert ("This is a valid ID"); }
else
}

But what goes in place of the ????




如果没有属于userID中包含的名称的属性,

尝试访问该属性将产生undefined。您可以通过比较类型来检查




if(''undefined''!= typeof user [userId]){

//有效身份证件

}


希望有所帮助,

Mike


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。



If there is no property that goes by the name contained in userID,
attempting to access that property will yield undefined. You can check for
that by comparing the type:

if(''undefined'' != typeof user[userId]) {
// valid ID
}

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


Michael Winter写道:
Michael Winter wrote:
2004年11月17日星期三20:21:24 +0000(UTC) ,JGH< jo ****** @ nospam.tds.net>
写道:
On Wed, 17 Nov 2004 20:21:24 +0000 (UTC), JGH <jo******@nospam.tds.net>
wrote:
function isUser(userID)
{
if(?????)
{alert(" This is a valid ID"); }

}

但代替????
function isUser (userID)
{
if (?????)
{ alert ("This is a valid ID"); }
else
}

But what goes in place of the ????



如果没有经过的财产userID中包含的名称,
尝试访问该属性将产生undefined。您可以通过比较类型来检查


if(''undefined''!= typeof user [userId]){
//有效ID
}


If there is no property that goes by the name contained in userID,
attempting to access that property will yield undefined. You can check
for that by comparing the type:

if(''undefined'' != typeof user[userId]) {
// valid ID
}




直到你注意到isUser(pop)警告pop的那一天为止。是一个

有效身份证,你可以使用这个解决方案。


为什么会这样?因为pop,(或push,length,slice ...)是Array

对象的成员,你可能会意外地落在它上面(它的typeof字符串是

"功能")。两个语法user.pop和user [" pop"]具有相似的

效果。


我建议你根据预期的类型检查类型用户[userId]:


if(''string''== typeof user [userId]){

//有效ID

}


使用数组作为关联容器有陷阱;你可能希望

读取错误名称的线程Arrays as hash tables

http://groups.google.com/groups?thre...gle.com&rnum=1


Alexis



Until the day you notice that isUser( "pop" ) alerts that "pop" is a
valid ID, you''ll get on fine with this solution.

Why so ? Because pop, (or push, length, slice...) is a member of Array
objects, and you can accidentally fall on it (and its typeof string is
"function"). The two syntaxes, user.pop and user["pop"], have similar
effects.

I advise you to check type against the expected type of user[userId]:

if( ''string'' == typeof user[userId] ) {
// valid ID
}

Using arrays as associative containers has pitfalls; you may wish to
read the misnamed thread "Arrays as hash tables"

http://groups.google.com/groups?thre...gle.com&rnum=1

Alexis


2004年11月18日星期四10:09:15 +0100,Alexis Nikichine

< al ************** @ somedomain.fr>写道:


[snip]
On Thu, 18 Nov 2004 10:09:15 +0100, Alexis Nikichine
<al**************@somedomain.fr> wrote:

[snip]
我建议你根据预期的用户类型[userId]检查类型:

if(''string''== typeof user [userId]){
//有效ID
}


是的,这样更好。我忘记了对象

将具有的现有属性,例如toString和prototype。

使用数组作为关联容器有陷阱[...]
I advise you to check type against the expected type of user[userId]:

if( ''string'' == typeof user[userId] ) {
// valid ID
}
Yes, that is better. I forgot about the existing properties that an object
will have, such as toString and prototype.
Using arrays as associative containers has pitfalls [...]




的确如此。请注意,Lasse和Douglas说的第一件事是

不要使用数组。请改用一个简单的物品。


[snip]


Mike


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。



Indeed. Notice that the first thing that both Lasse and Douglas said was
don''t use an array. Use a simple object instead.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


这篇关于检查键是否在关联数组中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆