Create a new dictionary with javascript

I am trying to figure out how to make C # a "function" in javascript, but I'm out of luck !?

Here is C #:

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

I want to think that I need to use the "new array", but I'm not sure how to do this?

Any help is appreciated and thanks in advance :-)

+4
source share
5 answers

In JavaScript, every object is a dictionary (associative array). Your example can be written as an 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"; 

This example shows that the values ​​(field values) of a dictionary (object) are usually obtained by writing a key (field name) using dot notation, but you can also use index notation, as is the case with C # dictionaries. Please note that JavaScript is a dynamic language, so the type of keys and values ​​can be any. So you can define a dictionary (object) like this:

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

Values ​​(field values) can also be other objects or functions. If the value is a function, it is called a JavaScript object method.

=== UPDATE ===

The following is an approximation of the C # Dictionary<TKey,TValue> in JavaScript (test page here ). This code works, but I highly recommend using JavaScript in JavaScript: objects can be used directly as associative arrays, there is no real need to use such a wrapper!

 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; }()); 

Test code:

 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()); 

Console output (press 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 
+3
source

Here is a great explanation: JavaScript Associative Arrays Demystified

 var parameters = new Array(); parameters['link'] = 'http://mysite.com'; parameters['name'] = 'This is an test'; 
+3
source

How to try like this: -

 var dict = []; dict.push({ key: "Yourkeyvalue", value: "value" }); 
+1
source

Just use the object:

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

Here you go (from the top of your head, so don't blame me for syntax errors if they appear).

This is the same as in C #, so I think you are looking for this.

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

or here is an alternative (maybe it depends on your coding style)

Create an array and add elements after.

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

Source: https://habr.com/ru/post/1499412/


All Articles