Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中创建哈希或字典对象[重复](How to create a hash or dictionary object in JavaScript [duplicate])

This question already has an answer here:

(这个问题已经在这里有了答案:)

I want to create a map object in javascript.

(我想在javascript中创建一个地图对象。)

I came to the following idea:

(我想到了以下想法:)

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

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

(但是那我怎么才能找到一个特定的键是否存在?)

  ask by Saurabh Kumar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...