对象作为关联数组的关键 [英] Object as key for associative array

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

问题描述

是否可以将一个Object作为关联数组的键

,如下例所示....

函数Obj(var1,var2)

{

this.var1 = var1;

this.var1 = var1;

}

function test(){

var sectionArray = new Array();

var obj = new Obj(" c"," k");

var obj2 = new Obj(3,n);

var obj3 = new Obj(v,qk);


sectionArray [obj] =" king";

sectionArray [obj2] =" queen" ;;

sectionArray [obj3 ] =" joker";

alert(sectionArray [obj]);

}


这不会给出任何错误但警报说小丑而不是

king。我的代码有什么问题或是对象的东西

只是不能用作密钥。


如果是后者可以有人请建议我做另一种方式

吗?我可以使用连接的String而不是对象吗?我唯一的

问题是如何从字符串中提取值,是什么?/ b $ b JavaScript有像Java这样的标记化方法吗?


感谢您的帮助。

Is it possible to have an Object as the key for an Associative array
like in the following example....
function Obj(var1, var2)
{
this.var1 = var1;
this.var1 = var1;
}
function test() {
var sectionArray = new Array();
var obj = new Obj("c","k");
var obj2 = new Obj("3","n");
var obj3 = new Obj("v","qk");

sectionArray[obj] = "king";
sectionArray[obj2] = "queen";
sectionArray[obj3] = "joker";
alert(sectionArray[obj]);
}

This does not give any error but the alert says "joker" instead of
"king". Is there anything wrong with my code or is an Object something
that just can not be used as a key.

If it is the latter can someone please suggest another way for me to do
it? Can I use a concatenated String instead of an object? My only
problem then is how would I extract the values from the String, does
JavaScript have a tokenizer method like Java?

Thanks for any help.

推荐答案

Je ******* @ aol.com 说:

是否可以将一个Object作为关联数组的键
如下例所示....

函数Obj(var1,var2)
{
this.var1 = var1;
this.var1 = var1;

}
function test(){
var sectionArray = new Array();
var obj = new Obj(" c"," k") ;
var obj2 = new Obj(3,n);
var obj3 = new Obj(v,qk);
sectionArray [obj] =" king" ;;
sectionArray [obj2] =" queen" ;;
sectionArray [obj3] =" joker";
alert(sectionArray [obj]) ;
}

这不发出任何错误,但警告说小丑而不是国王。我的代码有什么问题,或者是一个不能用作密钥的对象。

如果是后者可以请某人为我建议另一种方式<它?我可以使用连接的String而不是对象吗?我唯一的问题是如何从String中提取值,是否JavaScript有像Java这样的标记化器方法?

Is it possible to have an Object as the key for an Associative array
like in the following example....
function Obj(var1, var2)
{
this.var1 = var1;
this.var1 = var1;
}
function test() {
var sectionArray = new Array();
var obj = new Obj("c","k");
var obj2 = new Obj("3","n");
var obj3 = new Obj("v","qk");

sectionArray[obj] = "king";
sectionArray[obj2] = "queen";
sectionArray[obj3] = "joker";
alert(sectionArray[obj]);
}

This does not give any error but the alert says "joker" instead of
"king". Is there anything wrong with my code or is an Object something
that just can not be used as a key.

If it is the latter can someone please suggest another way for me to do
it? Can I use a concatenated String instead of an object? My only
problem then is how would I extract the values from the String, does
JavaScript have a tokenizer method like Java?




索引必须是一个字符串,因此Javascript使用默认的toString()方法将您的Object转换为字符串

,该方法返回[Object] (或类似

)。定义你自己的toString()方法,它返回一个唯一的值。


请注意,Javascript没有真正的关联数组。任何Object的

属性名称都可以用作它们返回

值的索引。



The index has to be a string, so Javascript converts your Object to a string
using the default toString() method, which returns "[Object]" (or something like
that). Define your own toString() method that returns a unique value.

And be aware that Javascript doesn''t have true associative arrays. The
attribute names of any Object can be used as if they were indices to return
their values.

如果您试图将对象放入一个没有

唯一键的关联数组中,那么您的概念可能有问题

数据结构体。也许尝试这样的东西,这是一个由字符串键入的对象的对象哈希值。 (注意JS1.2

对象文字/初始化语法允许你在线指定

属性和值。另外,请注意两种方法来引用

对象。)


var cards = {

" king":{num:13,let:" k" },

" queen":{num:12,let:" q" },

" jack":{num:11,let:" j" }

};


alert(''queen is:''+ cards [" queen"]。num +''''+ cards.queen .let);

If you''re trying to put objects into an associative array without a
unique key, then there is probably something wrong with your concept of
the data structure. Maybe try something like this, which is an
associative hash of objects, keyed by a string. (Note that JS1.2
object literal/initializer syntax allows you to specify both the
properties and values in-line. Also, note two ways to reference the
object.)

var cards = {
"king": { num:13, let:"k" },
"queen": { num:12, let:"q" },
"jack": { num:11, let:"j" }
};

alert(''queen is: ''+cards["queen"].num+'' ''+cards.queen.let);


你能告诉我如何定义toString()方法吗?

Can you show me how to define the toString () method ?


这篇关于对象作为关联数组的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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