Forum Discussion

fayrehouse's avatar
fayrehouse
Frequent Contributor
13 years ago

Iterate over System.Collections.Generic.Dictionary with JScript

Folks,



I have an object "xobj.Keys" with ClrFullClassName of:



System.Collections.Generic.Dictionary`2+KeyCollection[[iTextSharp.text.pdf.PdfName, itextsharp, Version=5.4.4.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca],[iTextSharp.text.pdf.PdfObject, itextsharp, Version=5.4.4.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca]]



I've seen scattered on the web sample code in C# to the effect of




foreach (PdfName name in xobj.Keys)



However, as we all know jscript does not support "foreach". I've tried:



  for (var thiskey in xobj.Keys)



But this produces an "object doesn't support this action" error.



Can someone enlighten me on how to iterate through a dictionary such as this using Jscript?



1 Reply

  • fayrehouse's avatar
    fayrehouse
    Frequent Contributor
    Again this was one of those situations where I battled with it for hours, posted the question here, THEN found the answer ;-)...



    For those of you wanting to battle dictionaries, which have no indices (preventing the 0..n approach), and since we have no for each equivalent - you might find something like this does the job:





        var enumerator = xobj.Keys.GetEnumerator();


        while (enumerator.MoveNext())


        {


          var name = enumerator.Current;


          var obj = xobj.Get(name);

           .....

         }