visit(callback, collection)

Iterates cells in row-major order and calls callback({ row, col, value }) for each cell whose value is contained in collection (Array or Set). Reading and mutating the grid inside the callback is allowed.

Example

(click to refill q with random emojis; monkey cells get highlighted)

edit, then run

Note

  • The first loop fills all cells with random emojis using visit(callback).
  • The second loop visits only monkey cells, marking them in a separate quadrille.
  • Membership is matched by identity (===) — for object values (colors, images), the collection must hold the same instances stored in the cells; the same story as strict search and the single-value filter.

Syntax

visit(callback, collection)

Parameters

Param Description
callback Function called for each visited cell, receiving an object { row, col, value }. Its return value is ignored
collection An Array or Set of values. Only cells whose value is in the collection are visited
What this page's examples use
p5.quadrille
drawQuadrille createQuadrille cellLength fill visit
p5.js
createCanvas background color random

Names the analysis can prove are called, and the line each one is documented on. Local variables and members of an unknown receiver are left out.