{
  "doc": {
    "id": "api/commands/each",
    "title": "each | Cypress Documentation",
    "description": "Iterate through an array like structure in Cypress (arrays or objects with a `length` property).",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/each.md",
    "version": "e6988a974973e9090ce70406c38cb2b9e0eac9fa",
    "updated_at": "2026-05-15T15:50:22.536Z",
    "headings": [
      {
        "id": "api/commands/each#each",
        "text": "each",
        "level": 1
      },
      {
        "id": "api/commands/each#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/each#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/each#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/each#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/each#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/each#dom-elements",
        "text": "DOM Elements",
        "level": 3
      },
      {
        "id": "api/commands/each#promises",
        "text": "Promises",
        "level": 3
      },
      {
        "id": "api/commands/each#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/each#return-early",
        "text": "Return early",
        "level": 3
      },
      {
        "id": "api/commands/each#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/each#requirements-learn-about-chaining-commands",
        "text": "Requirements Learn about chaining commands",
        "level": 3
      },
      {
        "id": "api/commands/each#assertions-learn-about-assertions",
        "text": "Assertions Learn about assertions",
        "level": 3
      },
      {
        "id": "api/commands/each#timeouts-learn-about-timeouts",
        "text": "Timeouts Learn about timeouts",
        "level": 3
      },
      {
        "id": "api/commands/each#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/each#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/each#syntax",
      "doc_id": "api/commands/each",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\n.each(callbackFn)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.get('ul>li').each(() => {...}) // Iterate through each 'li'cy.getCookies().each(() => {...}) // Iterate through each cookie\n```\n\n**Incorrect Usage**\n\n```\ncy.each(() => {...})            // Errors, cannot be chained off 'cy'cy.clock().each(() => {...})    // Errors, 'clock' does not yield an array\n```\n\n### Arguments\n\n**callbackFn _(Function)_**\n\nPass a function that is invoked with the following arguments:\n\n*   `value`\n*   `index`\n*   `collection`\n\n### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\n\n*   `.each()` yields the same subject it was given.\n*   It is [unsafe](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried) to chain further commands that rely on the subject after `.each()`.\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 133
    },
    {
      "id": "api/commands/each#usage",
      "doc_id": "api/commands/each",
      "heading": "Usage",
      "heading_level": 3,
      "content_markdown": "### Usage\n\n**Correct Usage**\n\n```\ncy.get('ul>li').each(() => {...}) // Iterate through each 'li'cy.getCookies().each(() => {...}) // Iterate through each cookie\n```\n\n**Incorrect Usage**\n\n```\ncy.each(() => {...})            // Errors, cannot be chained off 'cy'cy.clock().each(() => {...})    // Errors, 'clock' does not yield an array\n```\n",
      "section": "api",
      "anchors": [
        "usage"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 60
    },
    {
      "id": "api/commands/each#yields-learn-about-subject-management",
      "doc_id": "api/commands/each",
      "heading": "Yields Learn about subject management",
      "heading_level": 3,
      "content_markdown": "### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\n\n*   `.each()` yields the same subject it was given.\n*   It is [unsafe](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried) to chain further commands that rely on the subject after `.each()`.\n",
      "section": "api",
      "anchors": [
        "yields-learn-about-subject-management"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 40
    },
    {
      "id": "api/commands/each#examples",
      "doc_id": "api/commands/each",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### DOM Elements\n\n**Iterate over an array of DOM elements**\n\n```\ncy.get('ul>li').each(($el, index, $list) => {  // $el is a wrapped jQuery element  if ($el.someMethod() === 'something') {    // wrap this element so we can    // use cypress commands on it    cy.wrap($el).click()  } else {    // do something else  }})\n```\n\n**The original array is always yielded**\n\nNo matter what is returned in the callback function, `.each()` will always yield the original array.\n\n```\ncy.get('li')  .should('have.length', 3)  .each(($li, index, $lis) => {    return 'something else'  })  .then(($lis) => {    expect($lis).to.have.length(3) // true  })\n```\n\n### Promises\n\n**Promises are awaited**\n\nIf your callback function returns a `Promise`, it will be awaited before iterating over the next element in the collection.\n\n```\ncy.wrap([1, 2, 3]).each((num, i, array) => {  return new Cypress.Promise((resolve) => {    setTimeout(() => {      resolve()    }, num * 100)  })})\n```\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 192
    },
    {
      "id": "api/commands/each#dom-elements",
      "doc_id": "api/commands/each",
      "heading": "DOM Elements",
      "heading_level": 3,
      "content_markdown": "### DOM Elements\n\n**Iterate over an array of DOM elements**\n\n```\ncy.get('ul>li').each(($el, index, $list) => {  // $el is a wrapped jQuery element  if ($el.someMethod() === 'something') {    // wrap this element so we can    // use cypress commands on it    cy.wrap($el).click()  } else {    // do something else  }})\n```\n\n**The original array is always yielded**\n\nNo matter what is returned in the callback function, `.each()` will always yield the original array.\n\n```\ncy.get('li')  .should('have.length', 3)  .each(($li, index, $lis) => {    return 'something else'  })  .then(($lis) => {    expect($lis).to.have.length(3) // true  })\n```\n",
      "section": "api",
      "anchors": [
        "dom-elements"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 125
    },
    {
      "id": "api/commands/each#promises",
      "doc_id": "api/commands/each",
      "heading": "Promises",
      "heading_level": 3,
      "content_markdown": "### Promises\n\n**Promises are awaited**\n\nIf your callback function returns a `Promise`, it will be awaited before iterating over the next element in the collection.\n\n```\ncy.wrap([1, 2, 3]).each((num, i, array) => {  return new Cypress.Promise((resolve) => {    setTimeout(() => {      resolve()    }, num * 100)  })})\n```\n",
      "section": "api",
      "anchors": [
        "promises"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 64
    },
    {
      "id": "api/commands/each#rules",
      "doc_id": "api/commands/each",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)\n\n*   `.each()` requires being chained off a previous command.\n\n### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions)\n\n*   `.each()` will only run assertions you have chained once, and will not [retry](/llm/markdown/app/core-concepts/retry-ability.md).\n\n### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts)\n\n*   `.each()` can time out waiting for a promise you've returned to resolve.\n",
      "section": "api",
      "anchors": [
        "rules"
      ],
      "path": "/llm/json/chunked/api/commands/each.json",
      "token_estimate": 72
    }
  ]
}