documentationfor yFiles for HTML 2.6

IEnumerable<T>

Defines ways to iterate over the items contained in this type by providing a method to get an IEnumerator<T>.

Remarks

This is a convertible type that can be used in the following alternative ways in parameter lists, parameter objects, and setters.

It is possible to specify an Array or to use the native 'iterable' protocol, iterators, or generators to define the IEnumerable<T>:

// Array
object.property = [item1, item2]

// Iterables
object.property = new Set([item1, item2])
object.property = new Map([
  [key1, item1],
  [key2, item2]
]).entries()

// Generators
function* generator(i) {
  yield i
  yield i + 10
}

// via the iterator protocol (with invocation)
object.property = generator(10)

// via the generator function (will be executed immediately to populate the collection)
object.property = function* generateValues() {
  yield 1
  yield 2
}// Array
object.property = [item1, item2]

// Iterables
object.property = new Set([item1, item2])
object.property = new Map([
  [key1, item1],
  [key2, item2]
]).entries()

// Generators
function* generator(i: number) {
  yield i
  yield i + 10
}

// via the iterator protocol (with invocation)
object.property = generator(10)

// via the generator function (will be executed immediately to populate the collection)
object.property = function* generateValues() {
  yield 1
  yield 2
}

Note that when using native iterables, the resulting IEnumerables can only be enumerated once and will be empty the second time they are evaluated. Pass the generator function, instead, to cause reevaluation or cache the results in a list or other collection.

Examples

All enumerables automatically implement the EcmaScript "iterable protocol" if the platform supports the Symbol.iterator.
Basic for-of usage
const enumerable = IEnumerable.from([1, 2, 3, 4])

for (const item of enumerable) {
  console.log(item)
}
// 1 2 3 4
They also mimic many of the functions of the Array.prototype, but typically evaluate lazily.
Basic for-of usage
const enumerable = IEnumerable.from([1, 2, 3, 4])

enumerable.forEach((item) => console.log(item)) // 1 2 3 4

// accessing the index
enumerable.forEach((item, index) =>
  console.log(`Item #${index} is ${item}`)
)
Enumerables are very flexible with their various helper methods:
Advanced enumerable usage
const widestNodes = graphComponent.graph.nodes
  // group nodes by their width
  .groupBy(
    (n) => n.layout.width,
    (width, items) => ({ width, items })
  )
  // and order the groups by their size
  .orderByDescending((entry) => entry.items.size)
  // consider the largest two groups only
  .take(2)
  // and take all their items
  .flatMap((entry) => entry.items)

// check which nodes can be reached from the two largest groups
new Reachability({ startNodes: widestNodes })
  .run(graphComponent.graph)
  // and select all those nodes
  .reachableNodes.forEach((n) =>
    graphComponent.selection.setSelected(n, true)
  )const widestNodes = graphComponent.graph.nodes
  // group nodes by their width
  .groupBy(
    (n) => n.layout.width,
    (width, items) => ({ width, items })
  )
  // and order the groups by their size
  .orderByDescending((entry) => entry.items!.size)
  // consider the largest two groups only
  .take(2)
  // and take all their items
  .flatMap((entry) => entry.items)

// check which nodes can be reached from the two largest groups
new Reachability({ startNodes: widestNodes })
  .run(graphComponent.graph)
  // and select all those nodes
  .reachableNodes.forEach((n) =>
    graphComponent.selection.setSelected(n, true)
  )

Type Parameters

T
The type of the enumerable objects.

Type Details

yfiles module
core
yfiles-umd modules
All modules
Legacy UMD name
yfiles.collections.IEnumerable

See Also

Properties

Methods

Static Methods