2013-08-28 50 views
4

我想弄清楚如何在JavaScript中做一個C#「函數」,但我沒有運氣!?用javascript創建新詞典

這裏是C#:

var parameters = new Dictionary<string,string> 
{ 
    { "link", "http://mysite.com" }, 
    { "name", "This is an test" } 
}; 

我要認爲我必須使用「新陣」,但我不知道如何做到這一點?

任何幫助表示讚賞和感謝提前:-)

+0

你想在這裏做什麼?你不能在JavaScript中使用.NET類,所以你的問題看起來很奇怪。 –

回答

1

如何嘗試這樣的: -

var dict = []; 

dict.push({ 
    key: "Yourkeyvalue", 
    value: "value" 
}); 
+0

好的答案;)。你能解釋爲什麼這是正確的嗎? –

+0

我想在數組中創建兩個具有屬性的對象文字!我在錯誤的軌道上嗎? –

+0

不,這很好。這個或者一個關聯數組很好,但是如果你已經解釋了爲什麼它會起作用,它對Mansa會有幫助 –

3

在JavaScript中,每個object是一本字典(關聯數組)。你舉的例子可以寫成一個object literal

var parameters = { 
    link: "http://mysite.com", 
    name: "This is an test" 
}; 

// Read access 
console.log(parameters.link); // or: 
console.log(parameters["link"]); 

// Write access 
parameters.link = "http://yoursite.com"; // or: 
parameters["link"] = "http://yoursite.com"; 
parameters.status = "OK"; // or: 
parameters["status"] = "OK"; 

這個例子表明,字典(對象)的值(字段值)通常是通過使用點符號寫入鍵(字段名)訪問,但你可以也使用索引表示法,例如C#字典。請注意,JavaScript是一種動態語言,因此鍵和值的類型可以是任何東西。因此,您可以定義一個像這樣的字典(對象):

var parameters = { 
    link: "http://mysite.com", 
    name: "This is an test", 
    id: 3, 
    4: "$#!+", 
    5: 123456 
}; 

值(字段值)也可以是其他對象或函數。如果某個值是一個函數,則稱其爲JavaScript對象的方法

=== UPDATE ===

這裏是在JavaScript(測試頁是here)的C#Dictionary<TKey,TValue>類的近似。這段代碼可以工作,但我強烈建議用JavaScript的方式來處理JavaScript:對象可以直接用作關聯數組,但並不需要使用這種包裝器!

var Dictionary = (function() { 
    function Dictionary() { 
     if (!(this instanceof Dictionary)) 
      return new Dictionary(); 
    } 

    Dictionary.prototype.Count = function() { 
     var key, 
      count = 0; 

     for (key in this) { 
      if (this.hasOwnProperty(key)) 
       count += 1; 
     } 
     return count; 
    }; 

    Dictionary.prototype.Keys = function() { 
     var key, 
      keys = []; 

     for (key in this) { 
      if (this.hasOwnProperty(key)) 
       keys.push(key); 
     } 
     return keys; 
    }; 

    Dictionary.prototype.Values = function() { 
     var key, 
      values = []; 

     for (key in this) { 
      if (this.hasOwnProperty(key)) 
       values.push(this[key]); 
     } 
     return values; 
    }; 

    Dictionary.prototype.KeyValuePairs = function() { 
     var key, 
      keyValuePairs = []; 

     for (key in this) { 
      if (this.hasOwnProperty(key)) 
       keyValuePairs.push({ 
        Key: key, 
        Value: this[key] 
       }); 
     } 
     return keyValuePairs; 
    }; 

    Dictionary.prototype.Add = function(key, value) { 
     this[key] = value; 
    } 

    Dictionary.prototype.Clear = function() { 
     var key, 
      dummy; 

     for (key in this) { 
      if (this.hasOwnProperty(key)) 
       dummy = delete this[key]; 
     } 
    } 

    Dictionary.prototype.ContainsKey = function(key) { 
     return this.hasOwnProperty(key); 
    } 

    Dictionary.prototype.ContainsValue = function(value) { 
     var key; 

     for (key in this) { 
      if (this.hasOwnProperty(key) && this[key] === value) 
       return true; 
     } 
     return false; 
    } 

    Dictionary.prototype.Remove = function(key) { 
     var dummy; 

     if (this.hasOwnProperty(key)) { 
      dummy = delete this[key]; 
      return true; 
     } else 
      return false; 
    } 

    return Dictionary; 
}()); 

測試代碼:

var d = new Dictionary(), 
    i, 
    keyValuePair; 

console.clear(); 

// Adding new elements. 
d.Add("key1", "value1"); 
d.Add("key2", "value2"); 

// This also works. There is no KeyNotFoundException, 
// the key-value pairs are inserted silently. 
d["key3"] = "value3"; 
d["key4"] = "value4"; 

// Getting the number of key-value pairs. 
console.log("Count:", d.Count()); 

// Getting an array containing the keys. 
console.log("Keys:", d.Keys()); 

// Getting an array containing the values. 
console.log("Values:", d.Values()); 

// Iterating over the key-value pairs. 
for (i = 0; i < d.KeyValuePairs().length; i += 1) { 
    keyValuePair = d.KeyValuePairs()[i]; 
    console.log("#", i + 1, keyValuePair.Key, "=>", keyValuePair.Value); 
} 

// Determining whether the dictionary contains a given key. 
console.log("key2?", d.ContainsKey("key2")); 
console.log("keyhole?", d.ContainsKey("keyhole")); 

// Determining whether the dictionary contains a given value. 
console.log("value2?", d.ContainsValue("value2")); 
console.log("valuable?", d.ContainsValue("valuable")); 

// Removing a value with a given key. 
console.log("Removing key2:", d.Remove("key2")); 
console.log("Count after Remove():", d.Count()); 
console.log("Removing keyhole:", d.Remove("keyhole")); 
console.log("Count after Remove():", d.Count()); 

// Clearing all key-value pairs. 
d.Clear(); 
console.log("Count after Clear():", d.Count()); 

控制檯輸出(按F12):

Count: 4 
Keys: ["key1", "key2", "key3", "key4"] 
Values: ["value1", "value2", "value3", "value4"] 
# 1 key1 => value1 
# 2 key2 => value2 
# 3 key3 => value3 
# 4 key4 => value4 
key2? true 
keyhole? false 
value2? true 
valuable? false 
Removing key2: true 
Count after Remove(): 3 
Removing keyhole: false 
Count after Remove(): 3 
Count after Clear(): 0 
+0

我已經嘗試了上述所有內容,但我需要它是{string,string}。我怎麼做? – Mansa

+0

@Mansa我更新了我的答案。 – kol

1

只需使用一個OBJE ct:

var parameters = { 
    "link": "http://mysite.com", 
    "name": "This is an test" 
}; 
+0

這是這裏最好的一個,thx –

0

在這裏,你走了(離開我的頭頂,所以不要責怪我的語法錯誤,如果有的話)。

這是,你會在C#這樣做,我認爲你正在尋找這個確切的同樣的方法。

var parameters = [ 
    { 
     key: 'link', 
     value: 'http://mysite.com' 
    }, 
    { 
     key: 'name', 
     value: 'This is an test' 
    }]; 

或在這裏是一種替代(可能更喜歡它,這取決於你的編碼風格)

創建一個數組,並在之後添加的項目。

var parameters = []; 
parameters.push(
    { 
     key: 'link', 
     value: 'http://mysite.com' 
    }, 
    { 
     key: 'name', 
     value: 'This is an test' 
    });