{"id":1658,"date":"2026-09-21T09:00:00","date_gmt":"2026-09-21T03:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/"},"modified":"2026-11-15T11:32:53","modified_gmt":"2026-11-15T06:02:53","slug":"lwc-loops-for-each-iterator","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/","title":{"rendered":"LWC Loops: for:each and iterator Explained with Examples"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">LWC Loops let a component show a list, such as courses, students or enrolments, by repeating a block of the template once for each item in an array. LWC has two loop directives: for:each for simple lists, and iterator when you need to know the first or last item. Both need a key so the framework can update the list efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will learn both loop directives, why keys matter, how to style the first and last rows, and how to handle clicks on a single row. It builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-data-binding-getters-conditional-rendering\/\">LWC data binding and getters<\/a>, <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-loops-for-while-do-while\/\">Apex loops<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/\">Apex collections<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are LWC Loops?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LWC Loops are template directives that render a block once for every element of an array property. You put for:each or iterator on a nested <code>&lt;template&gt;<\/code> tag, and the block inside repeats. Each repeated element needs a unique key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a printing press for name badges. You load one badge design and a list of names, and the press prints one badge per name. The design is the template block, the list is the array, and the key is the badge number that keeps each one unique.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why LWC Loops Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Almost every business screen shows a list. Here is why LWC Loops matter at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the course catalog lists every open course as a card.<\/li>\n\n<li>Also, a tutor&#8217;s page shows enrolled students in a simple list.<\/li>\n\n<li>Next, the first item in a ranking should look special.<\/li>\n\n<li>Each row needs its own button, such as Enrol or Remove.<\/li>\n\n<li>Finally, lists change often, so updates must be fast.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So LWC Loops turn arrays of records into readable screens.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How LWC Loops Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LWC Loops follow the same steps every time. First, the class holds an array, such as courses. Next, the template adds a nested template with for:each={courses} and for:item=&#8221;course&#8221;. Then LWC repeats the block once for each element. After that, each repeated element uses its key to track identity. Finally, when the array changes, LWC compares keys and updates only the rows that changed.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-1-1024x640.png\" alt=\"LWC Loops diagram showing array, template, repeat, key and update\" class=\"wp-image-1656\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How LWC repeats and updates list rows.<\/figcaption><\/figure>\n\n\n<h3 class=\"wp-block-heading\">for:each<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">for:each={courses} names the array, and for:item=&#8221;course&#8221; names the current item. Optionally, for:index=&#8221;index&#8221; gives the position, starting at 0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">iterator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">iterator:it={courses} gives an object for each item with it.value, it.index, it.first and it.last. Use it when the first or last row needs special handling.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The key Attribute<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The first element inside the loop needs key={course.Id} or another unique value. Keys must be strings or numbers. Avoid using the index as a key when items can be added, removed or reordered.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Row Events<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To know which row was clicked, put the record Id in a data attribute, such as data-id={course.Id}. Then read event.currentTarget.dataset.id in the handler.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">for:each vs iterator<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares the two loop directives.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>for:each<\/th><th>iterator<\/th><\/tr><\/thead><tbody><tr><td>Syntax<\/td><td>for:each and for:item<\/td><td>iterator:name<\/td><\/tr><tr><td>Current item<\/td><td>course<\/td><td>it.value<\/td><\/tr><tr><td>Index<\/td><td>Optional for:index<\/td><td>it.index<\/td><\/tr><tr><td>First and last flags<\/td><td>No<\/td><td>it.first and it.last<\/td><\/tr><tr><td>Best for<\/td><td>Most lists<\/td><td>Special first or last rows<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is simple. Use for:each for most lists. Use iterator only when the first or last item needs different markup.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-2-1024x640.png\" alt=\"LWC Loops diagram comparing for:each and iterator\" class=\"wp-image-1657\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">for:each vs iterator.<\/figcaption><\/figure>\n\n\n<h2 class=\"wp-block-heading\">Loop Variables at a Glance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the values available inside each loop.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Inside for:each<\/th><th>Inside iterator:it<\/th><\/tr><\/thead><tbody><tr><td>course (for:item)<\/td><td>it.value<\/td><\/tr><tr><td>index (for:index)<\/td><td>it.index<\/td><\/tr><tr><td>Not available<\/td><td>it.first<\/td><\/tr><tr><td>Not available<\/td><td>it.last<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real LWC Loops Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s use LWC Loops to build a course list at Bright Path Tutors. Each row shows the course and an Enrol button, and the first row gets a &#8220;Most popular&#8221; label.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import { LightningElement } from 'lwc';\n\nexport default class CourseList extends LightningElement {\n    courses = [\n        { Id: 'a01A', Name: 'Apex Basics', Fee__c: 5000 },\n        { Id: 'a01B', Name: 'SOQL Basics', Fee__c: 3000 },\n        { Id: 'a01C', Name: 'LWC Basics', Fee__c: 6000 }\n    ];\n    selected = '';\n\n    handleEnrol(event) {\n        const courseId = event.currentTarget.dataset.id;\n        const course = this.courses.find(c =&gt; c.Id === courseId);\n        this.selected = `You chose ${course.Name}.`;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The class holds a fixed array for now. Later posts load the same shape from Apex. Then handleEnrol reads the Id from the clicked button and finds the matching course.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: The Template with iterator<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n    &lt;lightning-card title=\"Courses\"&gt;\n        &lt;ul class=\"slds-p-around_medium\"&gt;\n            &lt;template iterator:it={courses}&gt;\n                &lt;li key={it.value.Id} class=\"slds-m-bottom_small\"&gt;\n                    &lt;template lwc:if={it.first}&gt;\n                        &lt;lightning-badge label=\"Most popular\"&gt;&lt;\/lightning-badge&gt;\n                    &lt;\/template&gt;\n                    {it.value.Name}, fee {it.value.Fee__c}\n                    &lt;lightning-button label=\"Enrol\" data-id={it.value.Id}\n                        onclick={handleEnrol} class=\"slds-m-left_small\"&gt;&lt;\/lightning-button&gt;\n                &lt;\/li&gt;\n            &lt;\/template&gt;\n        &lt;\/ul&gt;\n        &lt;p class=\"slds-p-horizontal_medium\"&gt;{selected}&lt;\/p&gt;\n    &lt;\/lightning-card&gt;\n&lt;\/template&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The iterator repeats the li for each course. The key is the record Id, and it.first adds the badge to the first row only. Each button carries its course Id in data-id.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: The Same List with for:each<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template for:each={courses} for:item=\"course\"&gt;\n    &lt;li key={course.Id}&gt;{course.Name}, fee {course.Fee__c}&lt;\/li&gt;\n&lt;\/template&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This shorter version works when no row needs special treatment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: See It Work<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is what the list shows after clicking Enrol on the second row.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[Most popular] Apex Basics, fee 5000  [Enrol]\nSOQL Basics, fee 3000                 [Enrol]\nLWC Basics, fee 6000                  [Enrol]\nYou chose SOQL Basics.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The badge sits only on the first row, and the message names the clicked course.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">LWC Loops and Large Lists<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Rendering thousands of rows at once is slow. So for big data, show a page at a time, or use lightning-datatable with infinite loading. We cover performance in a later post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with LWC Loops<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Forgetting the key, which causes an error in the browser console.<\/li>\n\n<li>Using the index as the key for lists that change order.<\/li>\n\n<li>Putting for:each on a normal element instead of a nested template.<\/li>\n\n<li>Reading event.target instead of event.currentTarget for the data attribute.<\/li>\n\n<li>Changing the array with push without reassigning it.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for LWC Loops<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the record Id as the key.<\/li>\n\n<li>Prefer for:each, and use iterator only for first or last rows.<\/li>\n\n<li>Pass row Ids with data attributes and read them from dataset.<\/li>\n\n<li>Replace the array, such as with spread syntax, to update the list.<\/li>\n\n<li>Use lightning-datatable for large, tabular data.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add a Remove button to each row that deletes that course from the array. Hint: use filter to create a new array without the clicked Id, then assign it to this.courses.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on LWC Loops<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Loops come up in nearly every LWC coding round. Here are the 21 questions asked most about LWC Loops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on LWC Loops<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: Which directives create loops in LWC?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">for:each with for:item, and iterator.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: Where do you place a loop directive?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On a nested template tag that wraps the repeated block.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What is the key attribute for?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It gives each item a unique identity, so LWC can update rows efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: Which element needs the key?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first element inside the loop template.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: How do you get the index in for:each?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add for:index=&#8221;index&#8221; next to for:item.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: What does iterator give you that for:each does not?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It gives the first and last flags, it.first and it.last.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: How do you read the current item in an iterator?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use it.value, where it is the iterator name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on LWC Loops<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Why should you avoid the index as a key?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because indexes change when items move, so LWC may update the wrong rows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: How do you know which row&#8217;s button was clicked?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Set data-id={record.Id} on the button and read event.currentTarget.dataset.id.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Why use currentTarget instead of target?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because target can be an inner element, while currentTarget is the element with the handler.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: Why did my list not update after push?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because the array reference stayed the same, so assign a new array instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How do you remove an item from a rendered list?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Filter it out into a new array and assign that array to the property.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: Can you nest loops?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, so you can loop over students inside each course, each with its own keys.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: How do you style only the last row?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use iterator and check it.last with lwc:if or in a getter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on LWC Loops<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: What types can a key be?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A string or a number. Objects are not allowed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: How does LWC use keys when the list changes?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It matches old and new keys, then only adds, removes or moves the changed rows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: How do you show computed values for each row?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Map the data into new objects with extra fields before rendering.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: When should you use lightning-datatable instead of a loop?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For large tabular data with sorting, selection or inline editing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How do you handle very long lists?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use pagination or infinite loading so the browser renders fewer rows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Can you use lwc:if inside a loop?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, so each row can show different blocks based on its data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: How do you show a message when the list is empty?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add a getter such as hasCourses and use lwc:if and lwc:else around the loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/lwc\/guide\/create-lists.html\" target=\"_blank\" rel=\"noopener noreferrer\">Render Lists (LWC Developer Guide)<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/lwc\/guide\/get-started-best-practices.html\" target=\"_blank\" rel=\"noopener noreferrer\">LWC Best Practices (LWC Developer Guide)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>LWC Loops repeat a template block for each array item.<\/li>\n\n<li>for:each suits most lists, and iterator adds first and last flags.<\/li>\n\n<li>Every row needs a unique key, ideally the record Id.<\/li>\n\n<li>Use data attributes and currentTarget to find the clicked row.<\/li>\n\n<li>Assign a new array so the list updates.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Components often pass data to their children. Next, read our guide to <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-parent-to-child-api\/\">LWC parent to child communication with @api<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LWC Loops let a component show a list, such as courses, students or enrolments, by repeating a block of the template once for each&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1655,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[245,8,96],"tags":[34,26,113],"swapz_series":[],"class_list":["post-1658","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lightning-web-components","category-salesforce","category-salesforce-developer","tag-certification","tag-intermediate","tag-lwc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>LWC Loops: for:each and iterator Explained<\/title>\n<meta name=\"description\" content=\"LWC Loops explained simply: for:each, for:item, for:index, iterator with first and last, the key attribute, nested lists and 21 interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LWC Loops: for:each and iterator Explained\" \/>\n<meta property=\"og:description\" content=\"LWC Loops explained simply: for:each, for:item, for:index, iterator with first and last, the key attribute, nested lists and 21 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-21T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T06:02:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"LWC Loops: for:each and iterator Explained with Examples\",\"datePublished\":\"2026-09-21T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:02:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/\"},\"wordCount\":1513,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/lwc-loops-for-each-iterator-featured.png\",\"keywords\":[\"Certification\",\"Intermediate\",\"LWC\"],\"articleSection\":[\"Lightning Web Components\",\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/\",\"name\":\"LWC Loops: for:each and iterator Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/lwc-loops-for-each-iterator-featured.png\",\"datePublished\":\"2026-09-21T03:30:00+00:00\",\"dateModified\":\"2026-11-15T06:02:53+00:00\",\"description\":\"LWC Loops explained simply: for:each, for:item, for:index, iterator with first and last, the key attribute, nested lists and 21 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/lwc-loops-for-each-iterator-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/lwc-loops-for-each-iterator-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"LWC Loops featured image: title card with an LWC code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-loops-for-each-iterator\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LWC Loops: for:each and iterator Explained with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LWC Loops: for:each and iterator Explained","description":"LWC Loops explained simply: for:each, for:item, for:index, iterator with first and last, the key attribute, nested lists and 21 interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/","og_locale":"en_US","og_type":"article","og_title":"LWC Loops: for:each and iterator Explained","og_description":"LWC Loops explained simply: for:each, for:item, for:index, iterator with first and last, the key attribute, nested lists and 21 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-21T03:30:00+00:00","article_modified_time":"2026-11-15T06:02:53+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"LWC Loops: for:each and iterator Explained with Examples","datePublished":"2026-09-21T03:30:00+00:00","dateModified":"2026-11-15T06:02:53+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/"},"wordCount":1513,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-featured.png","keywords":["Certification","Intermediate","LWC"],"articleSection":["Lightning Web Components","Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/","url":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/","name":"LWC Loops: for:each and iterator Explained","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-featured.png","datePublished":"2026-09-21T03:30:00+00:00","dateModified":"2026-11-15T06:02:53+00:00","description":"LWC Loops explained simply: for:each, for:item, for:index, iterator with first and last, the key attribute, nested lists and 21 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-loops-for-each-iterator-featured.png","width":2400,"height":1260,"caption":"LWC Loops featured image: title card with an LWC code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/lwc-loops-for-each-iterator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"LWC Loops: for:each and iterator Explained with Examples"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1658"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1658\/revisions"}],"predecessor-version":[{"id":2315,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1658\/revisions\/2315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1655"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1658"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}