{"id":164951,"date":"2009-11-11T17:10:14","date_gmt":"2009-11-11T17:10:14","guid":{"rendered":"http:\/\/www.esri.com\/arcgis-blog\/?post_type=blog&#038;p=164951"},"modified":"2018-12-18T10:45:13","modified_gmt":"2018-12-18T18:45:13","slug":"looping-through-graphics-using-the-arcgis-api-for-javascript","status":"publish","type":"blog","link":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript","title":{"rendered":"Looping through graphics using the ArcGIS API for JavaScript"},"author":4801,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"_acf_changed":false,"_searchwp_excluded":""},"categories":[22941],"tags":[33811,27451,24921,26501],"industry":[],"product":[36831,36601],"class_list":["post-164951","blog","type-blog","status-publish","format-standard","hentry","category-mapping","tag-dojo","tag-graphics","tag-javascript","tag-services","product-js-api-arcgis","product-developers"],"acf":{"short_description":"When working with the ArcGIS API for JavaScript you\u2019ll often find yourself looping through arrays, like the collection of graphics stored","flexible_content":[{"acf_fc_layout":"content","content":"<p>When working with the\u00a0<a href=\"http:\/\/resources.esri.com\/arcgisserver\/apis\/javascript\/arcgis\/index.cfm?fa=home\">ArcGIS API for JavaScript<\/a>\u00a0you\u2019ll often find yourself looping through arrays, like the collection of graphics stored in a graphics layer or the results of a QueryTask. In this post we\u2019ll explore two approaches for looping through arrays; using a standard for loop and working with a dojo implementation of one of the JavaScript Array Extras.<\/p>\n<h3>For Loop<\/h3>\n<p>First lets look at using a for loop to add query task results to a map. The results of QueryTask are returned as an array of Graphic features so we can use a for loop to iterate through the task results and add each graphic to the map.<\/p>\n<pre> dojo.connect(queryTask,\"onComplete\",function(featureSet){\r\n   for(var i=0; i &lt; featureSet.features.length; i++){\r\n     map.graphics.add(featureSet.features[i]);\r\n   })<\/pre>\n<p>We can optimize the code a bit by storing the length of the array in a variable. Then the length of the array doesn\u2019t need to be recalculated on each loop. The stored length (il) is just compared to the value of the counter(i).<\/p>\n<pre> for (var i=0; il = featureSet.features.length; i &lt; il; i++){<\/pre>\n<h3>JavaScript Array Extras<\/h3>\n<p>The JavaScript array extras are useful functions for working with arrays and are part of\u00a0<a href=\"http:\/\/www.ecma-international.org\/news\/PressReleases\/PR_Ecma_finalises_major_revision_of_ECMAScript.htm\">ECMAScript 5th Edition,<\/a>\u00a0the next revision of the standards on which JavaScript is based. The revised standard is still under development so not all browsers support this functionality. Thankfully Dojo has implemented similar functions as part of\u00a0<a href=\"http:\/\/dojotoolkit.org\/projects\/core\">Dojo Core<\/a>\u00a0so we can take advantage of one of the array extras,\u00a0<a class=\"\" href=\"http:\/\/docs.dojocampus.org\/dojo\/forEach\">dojo.forEach<\/a>, to simplify the process of looping through an array of graphics. Dojo.forEach runs a function on each item in the array.<\/p>\n<pre> dojo.connect(queryTask,\"onComplete\",function(featureSet){\r\n   dojo.forEach (featureSet.features, function(feature) {\r\n     map.graphics.add(feature);\r\n   });\r\n });<\/pre>\n<p>Note that we no longer need the counter and array length variable which makes the code easier to read and can help avoid the following problems:<\/p>\n<ul>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Off-by-one_error\">Off-by-one errors<\/a><\/li>\n<li>Overwriting a global variable by forgetting to declare the loop counter as a local variable using the var keyword.<\/li>\n<\/ul>\n<p>If you do need access to the iterator you can specify an additional argument to the function. This argument provides access to the current position in the array.<\/p>\n<pre> dojo.forEach(featureSet.features,function(feature,index){\r\n   \/\/do something with the index here<\/pre>\n<pre> }<\/pre>\n<h3>Links<\/h3>\n<ul>\n<li>Click\u00a0<a href=\"http:\/\/serverapps.esri.com\/javascript_examples\/UsingDojoForEach.html\">here<\/a>\u00a0to view a live sample that uses dojo.forEach to add graphics to a map.<\/li>\n<li>Information from\u00a0<a href=\"http:\/\/docs.dojocampus.org\/quickstart\/arrays?action=show&amp;redirect=dojo\/_base\/array\">Dojo<\/a>\u00a0on working with array utilities<\/li>\n<li>Details on\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/ECMAScript\">ECMAScript<\/a><\/li>\n<\/ul>\n<p><i>Contributed by Kelly Hutchins of the ArcGIS JavaScript API development team<\/i><\/p>\n"}],"authors":[{"ID":4801,"user_firstname":"Kelly","user_lastname":"Hutchins","nickname":"Kelly","user_nicename":"kelly","display_name":"Kelly Hutchins","user_email":"khutchins@esri.com","user_url":"","user_registered":"2018-03-02 00:16:25","user_description":"Software developer on the instant apps team building apps that help you share your Online content. Outside of work I enjoy spending time trail running with my dogs.","user_avatar":"<img data-del=\"avatar\" src='https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/04\/IMG_3695-213x200.jpg' class='avatar pp-user-avatar avatar-96 photo ' height='96' width='96'\/>"}],"related_articles":"","card_image":false,"wide_image":false},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Looping through graphics using the ArcGIS API for JavaScript<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Looping through graphics using the ArcGIS API for JavaScript\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript\" \/>\n<meta property=\"og:site_name\" content=\"ArcGIS Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/esrigis\/\" \/>\n<meta property=\"article:modified_time\" content=\"2018-12-18T18:45:13+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@ESRI\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript\"},\"author\":{\"name\":\"Kelly Hutchins\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/6a3c399b819d64e44070cdbedbf2cc9b\"},\"headline\":\"Looping through graphics using the ArcGIS API for JavaScript\",\"datePublished\":\"2009-11-11T17:10:14+00:00\",\"dateModified\":\"2018-12-18T18:45:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript\"},\"wordCount\":9,\"publisher\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#organization\"},\"keywords\":[\"Dojo\",\"Graphics\",\"JavaScript\",\"services\"],\"articleSection\":[\"Mapping\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript\",\"name\":\"Looping through graphics using the ArcGIS API for JavaScript\",\"isPartOf\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#website\"},\"datePublished\":\"2009-11-11T17:10:14+00:00\",\"dateModified\":\"2018-12-18T18:45:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.esri.com\/arcgis-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Looping through graphics using the ArcGIS API for JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#website\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/\",\"name\":\"ArcGIS Blog\",\"description\":\"Get insider info from Esri product teams\",\"publisher\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.esri.com\/arcgis-blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#organization\",\"name\":\"Esri\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2018\/04\/Esri.png\",\"contentUrl\":\"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2018\/04\/Esri.png\",\"width\":400,\"height\":400,\"caption\":\"Esri\"},\"image\":{\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/esrigis\/\",\"https:\/\/x.com\/ESRI\",\"https:\/\/www.linkedin.com\/company\/5311\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/6a3c399b819d64e44070cdbedbf2cc9b\",\"name\":\"Kelly Hutchins\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/04\/IMG_3695-213x200.jpg\",\"contentUrl\":\"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/04\/IMG_3695-213x200.jpg\",\"caption\":\"Kelly Hutchins\"},\"description\":\"Software developer on the instant apps team building apps that help you share your Online content. Outside of work I enjoy spending time trail running with my dogs.\",\"url\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Looping through graphics using the ArcGIS API for JavaScript","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:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript","og_locale":"en_US","og_type":"article","og_title":"Looping through graphics using the ArcGIS API for JavaScript","og_url":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript","og_site_name":"ArcGIS Blog","article_publisher":"https:\/\/www.facebook.com\/esrigis\/","article_modified_time":"2018-12-18T18:45:13+00:00","twitter_card":"summary_large_image","twitter_site":"@ESRI","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript#article","isPartOf":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript"},"author":{"name":"Kelly Hutchins","@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/6a3c399b819d64e44070cdbedbf2cc9b"},"headline":"Looping through graphics using the ArcGIS API for JavaScript","datePublished":"2009-11-11T17:10:14+00:00","dateModified":"2018-12-18T18:45:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript"},"wordCount":9,"publisher":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/#organization"},"keywords":["Dojo","Graphics","JavaScript","services"],"articleSection":["Mapping"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript","url":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript","name":"Looping through graphics using the ArcGIS API for JavaScript","isPartOf":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/#website"},"datePublished":"2009-11-11T17:10:14+00:00","dateModified":"2018-12-18T18:45:13+00:00","breadcrumb":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.esri.com\/arcgis-blog\/products\/js-api-arcgis\/mapping\/looping-through-graphics-using-the-arcgis-api-for-javascript#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.esri.com\/arcgis-blog\/"},{"@type":"ListItem","position":2,"name":"Looping through graphics using the ArcGIS API for JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.esri.com\/arcgis-blog\/#website","url":"https:\/\/www.esri.com\/arcgis-blog\/","name":"ArcGIS Blog","description":"Get insider info from Esri product teams","publisher":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.esri.com\/arcgis-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.esri.com\/arcgis-blog\/#organization","name":"Esri","url":"https:\/\/www.esri.com\/arcgis-blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2018\/04\/Esri.png","contentUrl":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2018\/04\/Esri.png","width":400,"height":400,"caption":"Esri"},"image":{"@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/esrigis\/","https:\/\/x.com\/ESRI","https:\/\/www.linkedin.com\/company\/5311\/"]},{"@type":"Person","@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/6a3c399b819d64e44070cdbedbf2cc9b","name":"Kelly Hutchins","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.esri.com\/arcgis-blog\/#\/schema\/person\/image\/","url":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/04\/IMG_3695-213x200.jpg","contentUrl":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2024\/04\/IMG_3695-213x200.jpg","caption":"Kelly Hutchins"},"description":"Software developer on the instant apps team building apps that help you share your Online content. Outside of work I enjoy spending time trail running with my dogs.","url":""}]}},"text_date":"November 11, 2009","author_name":"Kelly Hutchins","author_page":false,"custom_image":"https:\/\/www.esri.com\/arcgis-blog\/app\/uploads\/2025\/08\/Newsroom-Keyart-Wide-1920-x-1080.jpg","primary_product":"ArcGIS Maps SDK for JavaScript","tag_data":[{"term_id":33811,"name":"Dojo","slug":"dojo","term_group":0,"term_taxonomy_id":33811,"taxonomy":"post_tag","description":"","parent":0,"count":3,"filter":"raw"},{"term_id":27451,"name":"Graphics","slug":"graphics","term_group":0,"term_taxonomy_id":27451,"taxonomy":"post_tag","description":"","parent":0,"count":4,"filter":"raw"},{"term_id":24921,"name":"JavaScript","slug":"javascript","term_group":0,"term_taxonomy_id":24921,"taxonomy":"post_tag","description":"","parent":0,"count":151,"filter":"raw"},{"term_id":26501,"name":"services","slug":"services","term_group":0,"term_taxonomy_id":26501,"taxonomy":"post_tag","description":"","parent":0,"count":20,"filter":"raw"}],"category_data":[{"term_id":22941,"name":"Mapping","slug":"mapping","term_group":0,"term_taxonomy_id":22941,"taxonomy":"category","description":"","parent":0,"count":2690,"filter":"raw"}],"product_data":[{"term_id":36831,"name":"ArcGIS Maps SDK for JavaScript","slug":"js-api-arcgis","term_group":0,"term_taxonomy_id":36831,"taxonomy":"product","description":"","parent":36601,"count":362,"filter":"raw"},{"term_id":36601,"name":"Developers","slug":"developers","term_group":0,"term_taxonomy_id":36601,"taxonomy":"product","description":"","parent":0,"count":763,"filter":"raw"}],"primary_product_link":"https:\/\/www.esri.com\/arcgis-blog\/?s=#&products=js-api-arcgis","_links":{"self":[{"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/blog\/164951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/users\/4801"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/comments?post=164951"}],"version-history":[{"count":0,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/blog\/164951\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/media?parent=164951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/categories?post=164951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/tags?post=164951"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/industry?post=164951"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/www.esri.com\/arcgis-blog\/wp-json\/wp\/v2\/product?post=164951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}