Collection
Collections are ordered sets of handles tied to one core; scalar reads and bulk writes have distinct contracts.
The public collection abstraction represents an ordered set of handles tied
to one core. cy.elements() returns all live elements with nodes first and
edges second; cy.nodes() and cy.edges() narrow the result. An element
lookup returns a collection of length zero or one, so the same methods work
for a missing id, one element or a larger traversal result. Empty collections
are valid values.
Collection APIs make the unit of work explicit. Methods such as forEach()
visit length-one element collections. map() returns a plain array in
collection order. first(), last() and eq(i) return collections, not
unwrapped element objects. Scalar queries such as id(), position() and
data() read from the first element; they answer undefined when there is no
first element. Bulk mutators apply to every element and typically return the
same collection for chaining.
const a = cy.getElementById('a');const adjacentNodes = a.neighborhood().nodes();const ids = adjacentNodes.map(node => node.id());
const totalWeight = cy.edges().reduce( (sum, edge) => sum + edge.data('weight'), 0,);#Function categories
- Metadata and identity — Inspect identity and collection membership.
- Iteration and aggregation — Visit, map, reduce and aggregate elements in collection order.
- Comparison — Compare collections without mutating the graph.
- Building and filtering — Build ordered sets and filter with queries or predicates.
- Data and scratch — Read and update application data or transient scratch on elements.
- Graph manipulation — Remove elements or change topology while respecting terminal removed handles.
- Position and dimensions — Read geometry and write node positions in the documented coordinate space.
- Edge points — Read rendered or model-space endpoints and route geometry.
- Selection — Inspect and change element selection and selectability.
- Grab, lock and pannability — Inspect active state and control which elements can move through gestures.
- Visibility and emphasis — Inspect visibility and core-owned emphasis state.
- Style — Read resolved styling and manage supported bypasses.
- Events — Bind event handlers to each element in a collection.
- Animation — Animate model position and supported style channels, with promise-based sequencing.
- Layout — Scope a layout to a collection and use shared placement helpers.
- Traversing — Follow incidence, directed reachability, edge relations and connectivity.
- Compound nodes — Traverse compound containment and manage collapse state.
- Algorithms — Choose graph algorithms by task; read each executor and cancellation contract.