JavaScript 假字典

发布于 2024-07-06 19:41:17 字数 345 浏览 7 评论 0原文

我已经声明了 Javascript 数组,这样我就可以通过键访问它们,但那是很久以前的事了,我已经忘记了我是如何做到的。

基本上,我有两个要存储的字段,一个唯一的键及其值。 我知道有一种方法可以做到这一点..类似:

var jsArray = new {key: 'test test', value: 'value value'},
              new {key: 'test 2', value: 'value 2'};

并访问类似:

value = jsArray[key]

有人可以提醒我吗?

I've declared Javascript arrays in such a way that I could then access them by a key, but it was a long time ago, and I've forgotten how I did it.

Basically, I have two fields I want to store, a unique key, and its value. I know there is a way to do it.. something like:

var jsArray = new {key: 'test test', value: 'value value'},
              new {key: 'test 2', value: 'value 2'};

and accessed like:

value = jsArray[key]

Can someone remind me?

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

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

发布评论

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

评论(4

森林散布 2024-07-13 19:41:17

您可以通过不同的方式做到这一点:

var a = {'a':0, 'b':1, 'c':2};

var b = new Array();
b['a'] = 0;
b['b'] = 1;
b['c'] = 2;

var c = new Object();
c.a = 0;
c.b = 1;
c.c = 2;

You can do it in different ways:

var a = {'a':0, 'b':1, 'c':2};

var b = new Array();
b['a'] = 0;
b['b'] = 1;
b['c'] = 2;

var c = new Object();
c.a = 0;
c.b = 1;
c.c = 2;
单调的奢华 2024-07-13 19:41:17
var myFancyDictionary = {
  key: 'value',
  anotherKey: 'anotherValue',
  youGet: 'the idea'
}
var myFancyDictionary = {
  key: 'value',
  anotherKey: 'anotherValue',
  youGet: 'the idea'
}
┊风居住的梦幻卍 2024-07-13 19:41:17

如果您已经在使用 Prototype,请尝试使用它的哈希值。 如果使用 jQuery,请尝试使用 Map。

If you are already using Prototype, try using its Hash. If using jQuery, try using Map.

你曾走过我的故事 2024-07-13 19:41:17

这是一个提供简单字典的 JavaScript 类。

if( typeof( rp ) == "undefined" ) rp = {};

rp.clientState = new function()
{
    this.items = new Object();
    this.length = 0;

    this.set = function( key, value )
    {
        if ( ! this.keyExists( key ) )
        {
            this.length++;
        }
        this.items[ key ] = value;    
    }

    this.get = function( key )
    {
        if ( this.keyExists( key ) )
        {
            return this.items[ key ];
        } 
    }

    this.keyExists = function( key )
    {
        return typeof( this.items[ key ] ) != "undefined"; 
    }

    this.remove = function( key )
    {
        if ( this.keyExists( key ) )
        {
            delete this.items[ key ];
            this.length--;   
            return true;
        }
        return false;
    }

    this.removeAll = function()
    {
        this.items = null;
        this.items = new Object();
        this.length = 0;
    }
}

使用示例:

// Add a value pair.
rp.clientState.set( key, value );

// Fetch a value.
var x = rp.clientState.Get( key );

// Check to see if a key exists.
if ( rp.clientState.keyExists( key ) 
{
    // Do something.
}

// Remove a key.
rp.clientState.remove( key );

// Remove all keys.
rp.clientState.removeAll();

Here is a JavaScript class that provides a simple dictionary.

if( typeof( rp ) == "undefined" ) rp = {};

rp.clientState = new function()
{
    this.items = new Object();
    this.length = 0;

    this.set = function( key, value )
    {
        if ( ! this.keyExists( key ) )
        {
            this.length++;
        }
        this.items[ key ] = value;    
    }

    this.get = function( key )
    {
        if ( this.keyExists( key ) )
        {
            return this.items[ key ];
        } 
    }

    this.keyExists = function( key )
    {
        return typeof( this.items[ key ] ) != "undefined"; 
    }

    this.remove = function( key )
    {
        if ( this.keyExists( key ) )
        {
            delete this.items[ key ];
            this.length--;   
            return true;
        }
        return false;
    }

    this.removeAll = function()
    {
        this.items = null;
        this.items = new Object();
        this.length = 0;
    }
}

Example use:

// Add a value pair.
rp.clientState.set( key, value );

// Fetch a value.
var x = rp.clientState.Get( key );

// Check to see if a key exists.
if ( rp.clientState.keyExists( key ) 
{
    // Do something.
}

// Remove a key.
rp.clientState.remove( key );

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