Iterating Over Objects

JavaScript 1.7 supports an Iterator class. This is used in Salesforce B2C Commerce for all Collection classes, for all Map classes, and for the Iterator (dw.util.Iterator) class. When the dw.util.Iterator class isn't imported, "Iterator" refers to the native Iterator class. If dw.util.Iterator is imported, for example, with importPackage( dw.util ), then "Iterator" refers to the dw.util.Iterator.

These are some code examples:


		// iteration over all values in a list
			var l = new ArrayList( ... );
			for each( e in l ) {
				trace( e );
			}

		// iteration over all keys in a map
			var m = new SortedMap();
			m.put( "Joe", "111" );
			m.put( "Mary", "222" );
			m.put( "Jim", "333" );
			for( k in m ) {
				trace( k );
			}

		// --> Jim, Joe, Mary
		// iteration over all values in a map
			for each( v in m ) {
				trace( v );
			}

		// --> 333, 111, 222
		// iterating a SeekableIterator in a loop
			var result = queryCustomObjects( ... );
			for each( c in result ) {
				... do something with c ...
			}

For more information about iterating over objects, see also Iteration and Transaction Boundaries and Creating Custom Business Objects with Scripts.