如何在JavaScript中创建哈希或字典对象[duplicate]

时间:2022-12-25 12:53:47

This question already has an answer here:

这个问题已经有了答案:

I want to create a map object in javascript. I came to the following idea:

我想用javascript创建一个映射对象。我的想法是:

 var a = new Array();
 a["key1"] = "value1";
 a["key2"] = "value2";

but then how I can find if a particular key exists or not?

但是我怎么才能找到某个键是否存在呢?

5 个解决方案

#1


263  

Don't use an array if you want named keys, use a plain object.

不要使用数组,如果你想要命名键,请使用普通对象。

var a = {};
a["key1"] = "value1";
a["key2"] = "value2";

Then:

然后:

if ("key1" in a) {
   // something
} else {
   // something else 
}

#2


42  

A built-in Map type is now available in JavaScript. It can be used instead of simply using Object. It is supported by current versions of all major browsers.

JavaScript中现在可以使用内置的映射类型。它可以被使用而不是简单地使用对象。所有主流浏览器的当前版本都支持它。

Maps do not support the [subscript] notation used by Objects. That syntax implicitly casts the subscript value to a primitive string or symbol. Maps support any values as keys, so you must use the methods .get(key), .set(key, value) and .has(key).

映射不支持对象使用的[下标]符号。该语法隐式地将下标值转换为原始字符串或符号。映射支持任何值作为键,因此必须使用.get(key)、.set(key, value)和.has(key)方法。

var m = new Map();
var key1 = 'key1';
var key2 = {};
var key3 = {};

m.set(key1, 'value1');
m.set(key2, 'value2');

console.assert(m.has(key2), "m should contain key2.");
console.assert(!m.has(key3), "m should not contain key3.");

Objects only supports primitive strings and symbols as keys, because the values are stored as properties. If you were using Object, it wouldn't be able to to distinguish key2 and key3 because their string representations would be the same:

对象只支持基本字符串和符号作为键,因为值存储为属性。如果使用对象,则不能区分key2和key3,因为它们的字符串表示是相同的:

var o = new Object();
var key1 = 'key1';
var key2 = {};
var key3 = {};

o[key1] = 'value1';
o[key2] = 'value2';

console.assert(o.hasOwnProperty(key2), "o should contain key2.");
console.assert(!o.hasOwnProperty(key3), "o should not contain key3."); // Fails!

Related

#3


38  

You want to create an Object, not an Array.

你想要创建一个对象,而不是一个数组。

Like so,

像这样,

var Map = {};

Map['key1'] = 'value1';
Map['key2'] = 'value2';

You can check if the key exists in multiple ways:

您可以通过多种方式检查密钥是否存在:

Map.hasOwnProperty(key);
Map[key] != undefined // For illustration // Edit, remove null check
if (key in Map) ...

#4


1  

Use the in operator: e.g. "key1" in a.

使用in运算符:例如。“key1”。

#5


0  

if( a['desiredKey'] !== undefined )
{
   // it exists
}

#1


263  

Don't use an array if you want named keys, use a plain object.

不要使用数组,如果你想要命名键,请使用普通对象。

var a = {};
a["key1"] = "value1";
a["key2"] = "value2";

Then:

然后:

if ("key1" in a) {
   // something
} else {
   // something else 
}

#2


42  

A built-in Map type is now available in JavaScript. It can be used instead of simply using Object. It is supported by current versions of all major browsers.

JavaScript中现在可以使用内置的映射类型。它可以被使用而不是简单地使用对象。所有主流浏览器的当前版本都支持它。

Maps do not support the [subscript] notation used by Objects. That syntax implicitly casts the subscript value to a primitive string or symbol. Maps support any values as keys, so you must use the methods .get(key), .set(key, value) and .has(key).

映射不支持对象使用的[下标]符号。该语法隐式地将下标值转换为原始字符串或符号。映射支持任何值作为键,因此必须使用.get(key)、.set(key, value)和.has(key)方法。

var m = new Map();
var key1 = 'key1';
var key2 = {};
var key3 = {};

m.set(key1, 'value1');
m.set(key2, 'value2');

console.assert(m.has(key2), "m should contain key2.");
console.assert(!m.has(key3), "m should not contain key3.");

Objects only supports primitive strings and symbols as keys, because the values are stored as properties. If you were using Object, it wouldn't be able to to distinguish key2 and key3 because their string representations would be the same:

对象只支持基本字符串和符号作为键,因为值存储为属性。如果使用对象,则不能区分key2和key3,因为它们的字符串表示是相同的:

var o = new Object();
var key1 = 'key1';
var key2 = {};
var key3 = {};

o[key1] = 'value1';
o[key2] = 'value2';

console.assert(o.hasOwnProperty(key2), "o should contain key2.");
console.assert(!o.hasOwnProperty(key3), "o should not contain key3."); // Fails!

Related

#3


38  

You want to create an Object, not an Array.

你想要创建一个对象,而不是一个数组。

Like so,

像这样,

var Map = {};

Map['key1'] = 'value1';
Map['key2'] = 'value2';

You can check if the key exists in multiple ways:

您可以通过多种方式检查密钥是否存在:

Map.hasOwnProperty(key);
Map[key] != undefined // For illustration // Edit, remove null check
if (key in Map) ...

#4


1  

Use the in operator: e.g. "key1" in a.

使用in运算符:例如。“key1”。

#5


0  

if( a['desiredKey'] !== undefined )
{
   // it exists
}