This project has moved. For the latest updates, please go here.

How to loop through IEnumerable in javascript

Mar 27, 2014 at 3:19 AM
Hello ClearScript,

I have a host object ProductManager and it provides a function to search products:
public IEnumerable<Product> Search(string condition);
How can I loop through the IEnumerable result in JavaScript likes following:
In C#:
ProductManager manager = new ProductManager();
engine.AddHostObject("productManager", manager );
engine.AddHostType("console", typeof(Console));
In JavaScript:
var results = productManager.Search("price > 100");
for (XXXXXXXX)   <- how to write the for statement here?
{
     ......
}
Should I use the ToArray() first just likes the following?
var results = productManager.Search("price > 100").ToArray();
for (var i = 0; i < results.Length; i++)
{
    console.WriteLine(results[i].Name);
}
Coordinator
Mar 27, 2014 at 2:42 PM
Greetings!

You should be able to use the IEnumerable interface directly. For example:
var results = productManager.Search('price > 100');
var enumerator = results.GetEnumerator();
while (enumerator.MoveNext()) {
    console.WriteLine(enumerator.Current.Name);
}
Thanks for your question, and good luck!