Skip to content
Jing Lu edited this page May 14, 2013 · 13 revisions

ReoScript provides iterating over an object if the .Net type of which object implements the IEnumerable interface.

Iterator Syntax

To iterate over an object you may use the for ... in syntax.

The array in ReoScript could be iterated on every elements. (not index like JavaScript)

var arr = ['a', 'b', 10, 30];

// iterate array
for (element in arr) {
  console.log(element);
}

The result is:

a
b
10
30

Iterating over an object to retrieve all of property names.

var obj = { a: 10, b: 20 };

for (key in obj) {
  console.log(key + " : " + obj[key]);
}

The result is:

a : 10
b : 20

String can also be iterated on every characters.

Enumerator Extension

.Net type could be implemented to support the iterator in script by IEnumerable interface.

public class StringObject : ObjectValue, IEnumerable
{
  public string String { get; set; }

  public override IEnumerator GetEnumerator()
  {
    for (int i = 0; i < String.Length; i++)
    {
      yield return String[i];
    }
  }
}

Now StringObject supported to iterate using for...in syntax:

var a = 'welcome';

for(c in a) {
  console.log(c);
}

The result is:

w
e
l
c
o
m
e