私はこのようなJavaScriptで辞書を作る必要があります
私は正確な表記を覚えていませんが、それは次のようなものでした:
states_dictionary={ CT=[alex,harry], AK=[liza,alex], TX=[fred, harry] ........ }
JavaScriptにそのようなものはありますか?
私はこのようなJavaScriptで辞書を作る必要があります
私は正確な表記を覚えていませんが、それは次のようなものでした:
states_dictionary={ CT=[alex,harry], AK=[liza,alex], TX=[fred, harry] ........ }
JavaScriptにそのようなものはありますか?
回答:
これは古い投稿ですが、とにかく図解の答えを出すべきだと思いました。
JavaScriptのオブジェクト表記を使用します。そのようです:
states_dictionary={
"CT":["alex","harry"],
"AK":["liza","alex"],
"TX":["fred", "harry"]
};
そして値にアクセスするには:
states_dictionary.AK[0] //which is liza
または、JavaScriptリテラルオブジェクト表記を使用して、キーを引用符で囲む必要がないようにすることもできます。
states_dictionary={
CT:["alex","harry"],
AK:["liza","alex"],
TX:["fred", "harry"]
};
Object.hasOwnProperty.call(dictionary, key)(そうでない場合、ユーザーはvalueOfの値を入力して、コードが期待するものとは異なるObjectプロトタイプに属する関数をdictionary['valueOf']返すObject.valueOf()可能性があります-潜在的なバグまたはセキュリティの問題)。キーが文字列型でない場合は注意が必要です。それ以外の場合は、暗黙的な数値およびtoString変換によって問題が発生します。ES6 Mapタイプは、辞書に拡張機能を提供するように設計されています。
2015年(ECMAScript 6のリリース)まで、Javascriptには実際の連想配列はありませんでした。それ以降、Robocatの状態としてMapオブジェクトを使用できます。MDNで詳細を調べてください。例:
let map = new Map();
map.set('key', {'value1', 'value2'});
let values = map.get('key');
ES6のサポートがなければ、オブジェクトを使用してみることができます。
var x = new Object();
x["Key"] = "Value";
ただし、オブジェクトでは、array.lengthなどの一般的な配列プロパティやメソッドを使用することはできません。少なくともfor-in-loopで「オブジェクト配列」にアクセスすることは可能です。
これは古い質問ですが、「javascript辞書」を検索するとGoogleにポップアップ表示されるので、ECMAScript 6ではMap辞書である公式オブジェクトが導入されたことを上記の回答に追加したいと思います実装:
var dict = new Map();
dict.set("foo", "bar");
//returns "bar"
dict.get("foo");
JavaScriptの通常のオブジェクトとは異なり、任意のオブジェクトをキーとして使用できます。
var foo = {};
var bar = {};
var dict = new Map();
dict.set(foo, "Foo");
dict.set(bar, "Bar");
//returns "Bar"
dict.get(bar);
//returns "Foo"
dict.get(foo);
//returns undefined, as {} !== foo and {} !== bar
dict.get({});
dict = { key: value)か?
ここでJSで簡単な辞書を作成しました:
function JSdict() {
this.Keys = [];
this.Values = [];
}
// Check if dictionary extensions aren't implemented yet.
// Returns value of a key
if (!JSdict.prototype.getVal) {
JSdict.prototype.getVal = function (key) {
if (key == null) {
return "Key cannot be null";
}
for (var i = 0; i < this.Keys.length; i++) {
if (this.Keys[i] == key) {
return this.Values[i];
}
}
return "Key not found!";
}
}
// Check if dictionary extensions aren't implemented yet.
// Updates value of a key
if (!JSdict.prototype.update) {
JSdict.prototype.update = function (key, val) {
if (key == null || val == null) {
return "Key or Value cannot be null";
}
// Verify dict integrity before each operation
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
var flag = false;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
this.Values[i] = val;
flag = true;
break;
}
}
if (!flag) {
return "Key does not exist";
}
}
}
// Check if dictionary extensions aren't implemented yet.
// Adds a unique key value pair
if (!JSdict.prototype.add) {
JSdict.prototype.add = function (key, val) {
// Allow only strings or numbers as keys
if (typeof (key) == "number" || typeof (key) == "string") {
if (key == null || val == null) {
return "Key or Value cannot be null";
}
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
return "Duplicate keys not allowed!";
}
}
this.Keys.push(key);
this.Values.push(val);
}
else {
return "Only number or string can be key!";
}
}
}
// Check if dictionary extensions aren't implemented yet.
// Removes a key value pair
if (!JSdict.prototype.remove) {
JSdict.prototype.remove = function (key) {
if (key == null) {
return "Key cannot be null";
}
if (keysLength != valsLength) {
return "Dictionary inconsistent. Keys length don't match values!";
}
var keysLength = this.Keys.length;
var valsLength = this.Values.length;
var flag = false;
for (var i = 0; i < keysLength; i++) {
if (this.Keys[i] == key) {
this.Keys.shift(key);
this.Values.shift(this.Values[i]);
flag = true;
break;
}
}
if (!flag) {
return "Key does not exist";
}
}
}
var dict = new JSdict();
dict.add(1, "one")
dict.add(1, "one more")
"Duplicate keys not allowed!"
dict.getVal(1)
"one"
dict.update(1, "onne")
dict.getVal(1)
"onne"
dict.remove(1)
dict.getVal(1)
"Key not found!"
これは単なる基本的なシミュレーションです。これは、より良い実行時間アルゴリズムを実装して、少なくともO(nlogn)時間以下の複雑さで機能するようにさらに最適化できます。配列のマージ/クイックソートのように、次にルックアップのB検索を行います。JSでのハッシュ関数のマッピングについて、試したり検索したりしませんでした。
また、JSdictオブジェクトのキーと値をプライベート変数に変換して、こっそりさせることができます。
お役に立てれば!
編集>>上記を実装した後、JSオブジェクトを連想配列として個人的に使用しました。
ただし、便利なハッシュテーブルエクスペリエンスにするために実際に役立つことが判明した2つのメソッドについて、特別に触れておきます。
Viz:dict.hasOwnProperty(key) と dict [key] を削除
この実装/使用法に関する優れたリソースとしてこの投稿を読んでください。 JavaScript連想配列で動的にキーを作成する
ありがとう!
古い質問ですが、最近AS3> JSの移植を行う必要がありました。速度を上げるために、JS用の簡単なAS3スタイルのディクショナリオブジェクトを作成しました。
http://jsfiddle.net/MickMalone1983/VEpFf/2/
知らなかった場合は、AS3辞書を使用すると、文字列だけでなく、任意のオブジェクトをキーとして使用できます。用途が分かればとても重宝します。
ネイティブオブジェクトほど高速ではありませんが、その点で大きな問題は見つかりませんでした。
API:
//Constructor
var dict = new Dict(overwrite:Boolean);
//If overwrite, allows over-writing of duplicate keys,
//otherwise, will not add duplicate keys to dictionary.
dict.put(key, value);//Add a pair
dict.get(key);//Get value from key
dict.remove(key);//Remove pair by key
dict.clearAll(value);//Remove all pairs with this value
dict.iterate(function(key, value){//Send all pairs as arguments to this function:
console.log(key+' is key for '+value);
});
dict.get(key);//Get value from key
Firefox 13以降では、Pythonのmapオブジェクトに類似したdictオブジェクトの実験的な実装が提供されています。仕様はこちら。
これはFirefoxでのみ使用できますが、の属性を使用するよりも見栄えがしますnew Object()。ドキュメントからの引用:
- オブジェクトにはプロトタイプがあるため、マップにはデフォルトのキーがあります。ただし、これはを使用してバイパスできます
map = Object.create(null)。- のキーはで
ObjectありStrings、の任意の値にすることができますMap。- のサイズ
Mapを手動で追跡する必要がある一方で、のサイズを簡単に取得できますObject。