Skip to content

Iteration and aggregation

Visit, map, reduce and aggregate elements in collection order.

#eles.size()

// The number of elements — the method form of length
eles.size(): number

The number of elements — the method form of length.

Returns number

the element count

Example

console.log(eles.size()); // 5

Example graph and handle setup

#eles.empty()

// Whether the collection holds no elements
eles.empty(): boolean

Whether the collection holds no elements.

Returns boolean

true when empty

Example

console.log(eles.empty()); // false

Example graph and handle setup

#eles.nonempty()

// Whether the collection holds at least one element
eles.nonempty(): boolean

Whether the collection holds at least one element.

Returns boolean

true when non-empty

Example

console.log(eles.nonempty()); // true

Example graph and handle setup

#eles.forEach(fn, thisArg?)

// Call fn for each element
eles.forEach(fn: ( ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => void | false, thisArg?: unknown): this

Call fn for each element. Returning false from the callback stops the iteration early.

With no thisArg the callback is plain-called, so this is undefined inside it — the callback receives each element as its first argument.

Parameters
NameDescription
fn: ( ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => void | false

( ele, i, eles); return false to stop

thisArg: unknown?

optional receiver for the callback

Returns this

this collection, for chaining

Example

const ids = [];
eles.forEach(ele => ids.push(ele.id()));
console.log(ids.length); // 5

Example graph and handle setup

Also spelled eles.each.

#eles.toArray()

// The elements as a plain array of length-1 collections
eles.toArray(): Collection<NodeData, EdgeData, Data>[]

The elements as a plain array of length-1 collections.

Returns Collection<NodeData, EdgeData, Data>[]

a new array of the members

Example

console.log(eles.toArray().map(ele => ele.id())); // ['a', 'b', 'c', 'ab', 'bc']

Example graph and handle setup

#eles.slice(start?, end?)

// A sub-range of the collection, with Array#slice semantics (negative indices count from the end)
eles.slice(start?: number, end?: number): Collection<NodeData, EdgeData, Data>

A sub-range of the collection, with Array#slice semantics (negative indices count from the end).

Parameters
NameDescription
start: number?Default: 0

first index, inclusive

end: number?Default: this.length

last index, exclusive

Returns Collection<NodeData, EdgeData, Data>

the sub-range as a new collection

Example

console.log(eles.slice().map(ele => ele.id())); // ["a","b","c","ab","bc"]

Example graph and handle setup

#eles.sort(sortFn)

// A copy sorted by a comparator
eles.sort(sortFn: ( a: Collection<NodeData, EdgeData, Data>, b: Collection<NodeData, EdgeData, Data>, ) => number): Collection<NodeData, EdgeData, Data>

A copy sorted by a comparator. Note that sort order is a property of the collection, not of drawing: draw order is structural and there is no z-index.

Parameters
NameDescription
sortFn: ( a: Collection<NodeData, EdgeData, Data>, b: Collection<NodeData, EdgeData, Data>, ) => number

( a, b) comparator over length-1 collections; a non-function is ignored and returns this collection unchanged

Returns Collection<NodeData, EdgeData, Data>

a new, sorted collection

Example

const sorted = eles.sort((a, b) => a.id().localeCompare(b.id()));
console.log(sorted.first().id()); // 'a'

Example graph and handle setup

#eles.eq(i)

// The element at an index, as a length-1 collection
eles.eq(i: number): Collection<NodeData, EdgeData, Data>

The element at an index, as a length-1 collection.

Parameters
NameDescription
i: number

the index

Returns Collection<NodeData, EdgeData, Data>

that element, or an empty collection when out of range

Example

console.log(eles.eq(0).id()); // 'a'

Example graph and handle setup

#eles.first()

// The first element, as a length-1 collection
eles.first(): Collection<NodeData, EdgeData, Data>

The first element, as a length-1 collection.

Returns Collection<NodeData, EdgeData, Data>

the first element, or an empty collection

Example

console.log(eles.first().map(ele => ele.id())); // ["a"]

Example graph and handle setup

#eles.last()

// The last element, as a length-1 collection
eles.last(): Collection<NodeData, EdgeData, Data>

The last element, as a length-1 collection.

Returns Collection<NodeData, EdgeData, Data>

the last element, or an empty collection

Example

console.log(eles.last().map(ele => ele.id())); // ["bc"]

Example graph and handle setup

#eles.map(fn, thisArg?)

// Map each element through fn into a plain array
eles.map<T>(fn: ( ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => T, thisArg?: unknown): T[]

Map each element through fn into a plain array.

Parameters
NameDescription
fn: ( ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => T

( ele, i, eles)

thisArg: unknown?

optional receiver for the callback

Returns T[]

an array of the results

Example

console.log(cy.nodes().map(node => node.id())); // ['a', 'b', 'c']

Example graph and handle setup

#eles.some(fn, thisArg?)

// Whether any element satisfies the predicate
eles.some(fn: EleFilterFn<NodeData, EdgeData, Data>, thisArg?: unknown): boolean

Whether any element satisfies the predicate. Short-circuits.

Parameters
NameDescription
fn: EleFilterFn<NodeData, EdgeData, Data>

( ele, i, eles) => boolean

thisArg: unknown?

optional receiver for the callback

Returns boolean

true when at least one element matches

Example

console.log(eles.some(ele => ele.isEdge())); // true

Example graph and handle setup

#eles.every(fn, thisArg?)

// Whether every element satisfies the predicate
eles.every(fn: EleFilterFn<NodeData, EdgeData, Data>, thisArg?: unknown): boolean

Whether every element satisfies the predicate. Short-circuits, and is vacuously true for an empty collection.

Parameters
NameDescription
fn: EleFilterFn<NodeData, EdgeData, Data>

( ele, i, eles) => boolean

thisArg: unknown?

optional receiver for the callback

Returns boolean

true when all elements match

Example

console.log(cy.nodes().every(ele => ele.isNode())); // true

Example graph and handle setup

#eles.reduce(fn, initial)

// Fold the collection into a single value
eles.reduce<T>(fn: ( acc: T, ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => T, initial: T): T

Fold the collection into a single value.

Parameters
NameDescription
fn: ( acc: T, ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => T

( accumulator, ele, i, eles)

initial: T

the starting accumulator (required, unlike Array#reduce)

Returns T

the final accumulator

Example

const count = eles.reduce((sum, ele) => sum + (ele.isNode() ? 1 : 0), 0);
console.log(count); // 3

Example graph and handle setup

#eles.max(valFn, thisArg?)

// The element maximizing valFn, with its value
eles.max(valFn: ( ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => number, thisArg?: unknown): { value: number; ele: Collection<NodeData, EdgeData, Data> | undefined }

The element maximizing valFn, with its value.

Parameters
NameDescription
valFn: ( ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => number

( ele, i, eles) => number

thisArg: unknown?

optional receiver for the callback

Returns { value: number; ele: Collection<NodeData, EdgeData, Data> | undefined }

{ value, ele }, or { value: -Infinity, ele: undefined } when the collection is empty

Example

const result = cy.nodes().max(node => node.data('score'));
console.log(result.value); // 3

Example graph and handle setup

#eles.min(valFn, thisArg?)

// The element minimizing valFn, with its value
eles.min(valFn: ( ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => number, thisArg?: unknown): { value: number; ele: Collection<NodeData, EdgeData, Data> | undefined }

The element minimizing valFn, with its value.

Parameters
NameDescription
valFn: ( ele: Collection<NodeData, EdgeData, Data>, i: number, eles: Collection<NodeData, EdgeData, Data>, ) => number

( ele, i, eles) => number

thisArg: unknown?

optional receiver for the callback

Returns { value: number; ele: Collection<NodeData, EdgeData, Data> | undefined }

{ value, ele }, or { value: Infinity, ele: undefined } when the collection is empty

Example

const result = cy.nodes().min(node => node.data('score'));
console.log(result.value); // 1

Example graph and handle setup

All guides · Alphabetical API index · Read continuously