0

What is the JavaScript equivalent function for CreateObject("Scripting.Dictionary")? I have to convert following two statements from VBScript to JavaScript, anyone can help me to find a solution.

Set oInvoicesToCreate = CreateObject("Scripting.Dictionary")
If Not oInvoicesToCreate.Exists(cInvoiceID) Then
     oInvoicesToCreate(CStr(cInvoiceID)) = ""
End If
Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67
Madura Harshana
  • 1,299
  • 8
  • 25
  • 40
  • `CreateObject()` won't work in newer IE's (>8). You've to replace that with `var oInvoicesToCreate = new ActiveXObject("Scripting.Dictionary")` – Teemu Jan 11 '13 at 18:29

2 Answers2

3
var oInvoicesToCreate = {};
if(oInvoicesToCreate[cInvoiceID] === undefined){
    oInvoicesToCreate[cInvoiceID] = "";
}

You probably don't want to check the hasOwnProperty method because you'll want to check if anything in the prototype chain has that property as well and not overwrite it. checking with the []s will let you know if any property on any prototype items have the property as well.

bluetoft
  • 5,373
  • 2
  • 23
  • 26
  • Since we seem to be writing the same answer, I'll remove mine, and just leave a link to another SO discussion that goes more in-depth on the different approaches and their pros/cons for checking existence in the array: http://stackoverflow.com/questions/1098040/checking-if-an-associative-array-key-exists-in-javascript – Chris Young Dec 18 '12 at 14:39
  • what is the meant of `Scripting.Dictionary`, is that a syntax ?how can we use it after converting ..? is that not required ? thanx everyone for answr – Madura Harshana Dec 18 '12 at 14:50
  • 2
    a Scripting Dictionary in vb is pretty much a key value dictionary: "key" => value. In javascript an object {} can server the same purpose. javascript is a dynamic language so this anonymous object {} can serve your purposes. Were there other functions you were trying to use from the scripting dictionary in vb? – bluetoft Dec 18 '12 at 17:52
  • Object literals have no prototype chain, so the discussion of whether to use `hasOwnProperty` is irrelevant. – Zev Spitz Feb 08 '18 at 14:04
0

As bluetoft says in this answer, in Javascript you can use a plain object instead. However, there are a few differences between them that you should be aware of:


First, a Dictionary's keys can be any type:

var dict = new ActiveXObject('Scripting.Dictionary');
dict(5) = 'Athens';
console.log(dict('5')); //prints undefined

whereas any value used for a Javascript object's key will be converted to a string first:

var obj = {};
obj[5] = 'Athens';
console.log(obj['5']); // prints 'Athens'

From MDN:

Please note that all keys in the square bracket notation are converted to String type, since objects in JavaScript can only have String type as key type. For example, in the above code, when the key obj is added to the myObj, JavaScript will call the obj.toString() method, and use this result string as the new key.


Second, it is possible to set a Dictionary to treat differently cased keys as the same key, using the CompareMode property:

var dict = new ActiveXObject('Scripting.Dictionary');
dict.CompareMode = 1;
dict('a') = 'Athens';
console.log(dict('A')); // prints 'Athens'

Javascript key access via [] doesn't support this, and if you want to treat differently-cased keys as the same, you'll have to convert the potential key to lowercase or uppercase before each read or write.


For your specific scenario, neither of these differences matter, because the keys are numeric strings (1) which have no case (2).

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136