cancel
Showing results for 
Search instead for 
Did you mean: 

Iterating collections

michaelc
Champ on-the-rise
Champ on-the-rise
having all sorts of problems with the backend JavaScript, it's not the JavaScript I know.

So the method I have used to iterate a collection are not working for me.

   for ( var i=0; i<context.properties.section.sections.length;i++){     
        x = context.properties.section.sections;    
   }

this does not fail, but it does not work.
I dumped context.properties.section.sections.length I expected it to be an int.
but it seems to be an object.

org.mozilla.javascript.Undefined@63a65974

So this should be javascript 101 but I don't seem to be getting it.
how do I iterate a collocation in backend javascript.
#list will do it in the freemaker.

2 REPLIES 2

michaelc
Champ on-the-rise
Champ on-the-rise
I think I have my answer, not what I expected.
I dumped the properties methods for a section and found.

indexOf,notifyAll,removeAll,containsAll,contains,empty,equals,notify,subList,class,set,isEmpty,add
size,iterator,clear,wait,listIterator,toString,retainAll …

size returned 7 good, but tried to use in a for loop and the loop failed.
this does seem to work.
   var itr = context.properties.section.sections.iterator();
   while(itr.hasNext()) {
       sectionObj = itr.next();
       sectionPath.push(sectionObj.path);
    }
  

So this is far more Java then JavaScript.

michaelc
Champ on-the-rise
Champ on-the-rise
ok this is Java and not javascript at all.
I had the following statement
       if (sectionObj.path == sectionPath){

       }
this was never true, even when the values matched.
then I did the following
if (sectionObj.path.equals(sectionPath){

and it worked as expected.