如何使用javascript将对象作为键存储在数组中

发布于 2024-11-27 01:40:25 字数 554 浏览 1 评论 0原文

可能的重复:
如何使用 javascript 在数组中存储对象 < /p>

i想要将对象存储为数组中的键,解决方案是什么。

var myTabObj = safari.application.activeBrowserWindow.activeTab;

我想将 myTabObj 作为键存储在数组中。我认为 myTabObj 对于 safari 浏览器选项卡来说是独一无二的。

如何在 Safari 扩展开发中设置选项卡特定值。

我需要立即解决,请帮助我。例如,我有 3 个参数要为每个选项卡动态设置什么是解决方案或任何替代解决方案。

提前致谢

Possible Duplicate:
How to store objects in a array using javascript

i want to store object as key in a array what is the solution.

var myTabObj = safari.application.activeBrowserWindow.activeTab;

I want to store myTabObj as key in a array. I think myTabObj is unique for safari browser tab specific.

OR

How to set tab specific values in safari extension development.

I want solution immidiately please help me. for example i have 3 params to set for every tab dynamically what is the solution or any alternative solutions.

Thanks in advance

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

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

发布评论

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

评论(2

你是暖光i 2024-12-04 01:40:25

首先,您应该使用对象(哈希)而不是数组。无论如何,你都会这样做——数组是 Javascript 中的一个对象(哈希)。您可以将数组视为 JS 中的对象(哈希),使用非数字值作为键,但这很混乱 - 您在对象原型中获得数组函数和 dproperties,例如 arr.length,但它们仅适用于存储在数组对象中的数字键。

示例(进入Firebug的JS控制台):

>>> a=[]
[]

>>> a[{}] = 42
42

>>> console.dir(a)      
[object Object]   42
>>> a.length
0
>>> a[{}]
42

首先我创建一个数组。然后,我在表示数组的对象中创建一个带有键“{},隐式调用 toString”的属性。由于这不是数字键,因此特定于数组的函数会忽略它 - 数组的长度为 0。但是,如最后一行所示,数组实例(与 JS 中的所有内容一样是一个对象)确实存储该属性。

其次,对象(哈希)。

键始终是字符串。这就是为什么有些人说你不能使用对象作为键 - 但事实并非如此!如果您在 Javascript 中使用非字符串值作为键,则会调用该对象的 toString() 方法。每个对象都有该方法,因为它是在 Object.prototype.toString 上定义的。但是,在不重写后备本机方法的情况下,您始终会为您拥有的任何对象获得完全相同的字符串。

示例(输入 Firebug 的 JS 控制台):

>>> a={}
Object {}

>>> a[{}] = 1
1

>>> console.dir(a)
[object Object]   1

>>> a[{prop:"text"}] = 1
>>> console.dir(a)
[object Object]   1

因此,您可以使用对象作为键,但您必须确保为您打算使用的所有对象实例定义了自己的 toString 函数。这些 toString 函数通常打印类似(“类”)名称加上实例中存储的(唯一)字符串 ID 值之类的内容。如果你的 toString 函数不能保证为不同的对象生成唯一的字符串,那么你就不能使用对象(实例)作为键,当然可以,但它显然有点无用,因为你不会有可靠的 1:1映射。

这不是具体的帮助,但我必须承认,我更喜欢提供有关语言如何工作的一般性建议 - 此外,对于提供具体的解决方案,我想说的是没有足够的(代码)上下文。无论如何,要将其存储在数组中

var arr = [];

arr.push(instance);

,只需使用您选择的 JS 库框架的 for 循环或“...each()”函数(如果您使用的话),然后迭代该数组。

First, you sgould use an object (hash) and not an array. You do that anyway - an array IS an object (hash) in Javascript. You can treat an array as an object (hash) in JS, using non-numeric values as key, but this is messy - you get the Array-functions an dproperties in the objects prototype, e.g. arr.length, but they only work on the numeric keys stored in the array-object.

Example (entered into the JS console of Firebug):

>>> a=[]
[]

>>> a[{}] = 42
42

>>> console.dir(a)      
[object Object]   42
>>> a.length
0
>>> a[{}]
42

First I create an array. Then I create a property with key "{} on which toString was called implicitly" in the object that represents the array. Since this is not a numeric key the array-specific functions ignore it - the array's length is 0. However, as the last line shows, the array-instance, which is an object like everything in JS, DOES store the property.

Second, objects (hashes).

Keys are always strings. That's why some people say you cannot use objects as keys - but that is not true! If you use a non-string value as key in Javascript, the toString() method of that object will be called. EVERY object has that method, because it is defined on Object.prototype.toString. However, without overriding that fallback native method you always get the exact same string for whatever object you have.

Example (entered into the JS console of Firebug):

>>> a={}
Object {}

>>> a[{}] = 1
1

>>> console.dir(a)
[object Object]   1

>>> a[{prop:"text"}] = 1
>>> console.dir(a)
[object Object]   1

So, you CAN use objects as keys, but you have to make sure to have your own toString function defined for all object instances you intent to use. Those toString functions usually print something like a ("class")name plus a (unique) string ID value stored in the instance. If oyur toString functions are not guaranteed to produce a unique string for different objects you cannot use the objects (instances) as keys, well, of course you can but it's obviously a little useless, since you won't have a reliable 1:1 mapping.

This is not concrete help, but I must admit I prefer giving such general advice about how the language works - besides, for giving a concrete solution there is not enough (code) context I'd say. Anyway, to store it in an array simply use

var arr = [];

arr.push(instance);

and then iterate over the array with a for-loop or an "...each()"-function of the JS library framework of your choice, if you use one.

橘虞初梦 2024-12-04 01:40:25

您可以使用我的 jshashtable 使用任意对象作为键来存储值。

You can use my jshashtable to store values using arbitrary objects as keys.

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