Forum Discussion

newbiexiii's avatar
newbiexiii
New Contributor
4 years ago

Loop/iterate over dictionary

I created a dictionary as follows and would like to loop over it and perform some actions.

 

var dict = new ActiveXObject("Scripting.Dictionary");

dict.add("a",1);

dict.add("b",2);

dict.add("c",3);

for (var key in dict){

 Log.message("key is "+key+" and value is "+dict[key])

}

 

But the program does not enter the loop.

How can i iterate over the dictionary in Testcomplete using Jscript?

  • Hi,

    I am not familiar with Dictionary, but I found one example:
    //JScript
    function ItemsDemo()
    {
    var a, d, i, s; // Create some variables.
    d = new ActiveXObject("Scripting.Dictionary");
    d.Add ("a", "Athens"); // Add some keys and items.
    d.Add ("b", "Belgrade");
    d.Add ("c", "Cairo");
    a = (new VBArray(d.Items())).toArray(); // Get the items.
    s = "";
    for (i in a) // Iterate the dictionary.
    {
    s += a[i] + "<br>";
    }
    return(s); // Return the results.
    }

    It appears that in your code the part to create an array with the needed items is missing.
    I've tried to modify your code and here is what I've got:
    function DictTest() {
    var dict, ar, typeIs, i
    dict = new ActiveXObject("Scripting.Dictionary");
    dict.add("a", 1);
    dict.add("b","2");
    dict.add("c","3");

    ar = (new VBArray(dict.Items())).toArray()

    for(i in ar) {
    Log.Message("inside for loop");
    Log.Message("the keys are "+ar[i]);
    var typeIs = typeof ar[i];
    Log.Message("the type of keys is "+ typeIs)
    }
    }
    It looks pretty fine on playback and you can find the results in the screenshot below:


    Does it help?

    Thanks

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Try

    for (var key in dict.keys)

     

    Does it help?

     

    • newbiexiii's avatar
      newbiexiii
      New Contributor

      It enters the loop but when it try to access the values it shows up as undefined

      var dict = new ActiveXObject("Scripting.Dictionary");
      dict.add("a",1);
      dict.add("b","2");
      dict.add("c","3");

      var keys = dict.Keys();
      Log.Message("the keys are "+keys[0]); -----> prints undefined
      var typeIs = typeof keys;
      Log.Message("the type of keys is "+typeIs) -----------> prints undefined

      for(var i=0; i<dict.Count; i++){
      Log.Message("inside for loop");
      Log.Message("key is "+keys[i]+" and value is "+sessionsData[keys[i]]); ---------> prints key is undefined and value is undefined
      }

      • KseniaSemina's avatar
        KseniaSemina
        SmartBear Alumni (Retired)

        Hi,

        I am not familiar with Dictionary, but I found one example:
        //JScript
        function ItemsDemo()
        {
        var a, d, i, s; // Create some variables.
        d = new ActiveXObject("Scripting.Dictionary");
        d.Add ("a", "Athens"); // Add some keys and items.
        d.Add ("b", "Belgrade");
        d.Add ("c", "Cairo");
        a = (new VBArray(d.Items())).toArray(); // Get the items.
        s = "";
        for (i in a) // Iterate the dictionary.
        {
        s += a[i] + "<br>";
        }
        return(s); // Return the results.
        }

        It appears that in your code the part to create an array with the needed items is missing.
        I've tried to modify your code and here is what I've got:
        function DictTest() {
        var dict, ar, typeIs, i
        dict = new ActiveXObject("Scripting.Dictionary");
        dict.add("a", 1);
        dict.add("b","2");
        dict.add("c","3");

        ar = (new VBArray(dict.Items())).toArray()

        for(i in ar) {
        Log.Message("inside for loop");
        Log.Message("the keys are "+ar[i]);
        var typeIs = typeof ar[i];
        Log.Message("the type of keys is "+ typeIs)
        }
        }
        It looks pretty fine on playback and you can find the results in the screenshot below:


        Does it help?

        Thanks

  • Hi, 

    I verified this solution: it is bit more convenient:

    function ItemsDemo()
    {
    var a, d, i, s; // Create some variables.
    d = new getActiveXObject("Scripting.Dictionary");
    d.Add ("a", "Athens"); // Add some keys and items.
    d.Add ("b", "Belgrade");
    d.Add ("c", "Cairo");
    d.Items().toArray().forEach(eachItem => {Log.Message(eachItem)})
    d.Keys().toArray().forEach(eachItem => {Log.Message(eachItem)})
    }