{"id":2402,"date":"2018-10-17T00:15:11","date_gmt":"2018-10-17T05:15:11","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2402"},"modified":"2018-10-17T00:16:48","modified_gmt":"2018-10-17T05:16:48","slug":"handlebars-how-to-iterate-through-an-array-of-objects","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/10\/17\/handlebars-how-to-iterate-through-an-array-of-objects\/","title":{"rendered":"Handlebars: How to iterate through an array of objects"},"content":{"rendered":"<p>When using the Handlebars.js templating system, a common need is to be able to iterate through an array of JavaScript objects in order to build an HTML list. This is useful for &#8216;Atomic&#8217; web programming, where a component is defined, and you may have a dynamic number of such components in your database. For example, let&#8217;s say I have created a component called &#8220;verse set&#8221; which is a list of six Bible verses in a box, with a title at the top:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2404 \" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set.png\" alt=\"\" width=\"241\" height=\"387\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set.png 419w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set-187x300.png 187w\" sizes=\"auto, (max-width: 241px) 100vw, 241px\" \/><\/p>\n<p>And when hovering over a verse reference, the verse text appears in a div:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2406 size-large\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set-hover-1024x664.png\" alt=\"\" width=\"676\" height=\"438\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set-hover-1024x664.png 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set-hover-300x194.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set-hover-768x498.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set-hover-676x438.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/10\/verse-set-hover.png 1086w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/p>\n<p>In Handlebars, this &#8220;component&#8221; can be created on the web page with this HTML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"verse-set\" id=\"{{setID}}\"&gt;\r\n  &lt;div class=\"verse-set-header\"&gt;&lt;span&gt;{{setTitle}}&lt;\/span&gt;&lt;\/div&gt;\r\n\t&lt;div class=\"verse-set-box\"&gt;\r\n\t  &lt;ul&gt;\r\n\t    {{#each verses}}\r\n\t    &lt;li&gt;{{ref}}&lt;div class=\"view-verse-text\"&gt;{{text}}&lt;\/div&gt;\r\n\t    {{\/each}}\r\n\t  &lt;\/ul&gt;\r\n\t &lt;\/div&gt;\r\n  &lt;div class=\"play-button\"&gt;Play This Set&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>This HTML template is supported by a data structure that looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\">let setData = {\r\n  'setID': 1,\r\n  'setTitle': 'Philippians 4',\r\n  'verses' :\r\n\t  [\r\n\t    {\"ref\": \"Philippians 4:4-5\", \"text\": \"Rejoice in the Lord always. I will say it again: Rejoice! Let your gentleness be evident to all. The Lord is near.\"},\r\n            {\"ref\": \"Philippians 4:6-7\", \"text\": \"Do not be anxious about anything, but in everything, by prayer and petition, with thanksgiving, present your requests to God. And the peace of God, which transcends all understanding, will guard your hearts and your minds in Christ Jesus.\"},\r\n\t    {\"ref\": \"Philippians 4:8\",   \"text\": \"Finally, brothers and sisters, whatever is true, whatever is noble, whatever is right, whatever is pure, whatever is lovely, whatever is admirable-if anything is excellent or praiseworthy-think about such things.\"},\r\n\t    {\"ref\": \"Philippians 4:12\",  \"text\": \"I know what it is to be in need, and I know what it is to have plenty. I have learned the secret of being content in any and every situation, whether well fed or hungry, whether living in plenty or in want.\"},\r\n\t    {\"ref\": \"Philippians 4:13\",  \"text\": \"I can do all things through him who gives me strength.\"},\r\n\t    {\"ref\": \"Philippians 4:19\",  \"text\": \"And my God will meet all your needs according to his glorious riches in Christ Jesus.\" }\r\n\t  ]\r\n}<\/pre>\n<p>Notice it is an object with key-value pairs. One of the keys is called &#8216;verses&#8217; which is an array, which supports the {{#each verses}}\u00a0 block in Handlebars.<\/p>\n<p>Handlebars can support an array with a dynamic number of these components. All we have to do is push these objects into the array so that we now have a list of the above objects, then tell Handlebars to use this array. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let verseSetsData = []\r\n\r\nverseSetsData.push(setData)\r\nverseSetsData.push(setData)\r\nverseSetsData.push(setData)\r\nverseSetsData.push(setData)\r\nverseSetsData.push(setData)\r\nverseSetsData.push(setData)\r\n\r\n\/\/ Tells Handlebars to use this array of objects\r\nlet html = verseSetsGridCompiled(verseSetsData);<\/pre>\n<p>Then to have Handlebars iteratively place these objects in the grid:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">      {{#each this}}\r\n      &lt;div class=\"verse-set\" id=\"{{setID}}\"&gt;\r\n        &lt;div class=\"verse-set-header\"&gt;&lt;span&gt;{{setTitle}}&lt;\/span&gt;&lt;\/div&gt;\r\n        &lt;div class=\"verse-set-box\"&gt;\r\n          &lt;ul&gt;\r\n            {{#each verses}}\r\n            &lt;li&gt;{{ref}}&lt;div class=\"view-verse-text\"&gt;{{text}}&lt;\/div&gt;\r\n            {{\/each}}\r\n          &lt;\/ul&gt;\r\n         &lt;\/div&gt;\r\n        &lt;div class=\"play-button\"&gt;Play This Set&lt;\/div&gt;\r\n      &lt;\/div&gt;\r\n      {{\/each}}<\/pre>\n<p>Notice the word &#8216;this&#8217;, as in {{#each this}}. This is because the Handlebars context is now the array of objects, and since the context is an array, we don&#8217;t need key word substitution, but rather a simple &#8216;for loop&#8217; over the array. The &#8216;this&#8217; key word allows #each to work on an array context.<\/p>\n<p>For more information about Handlebars, see:<br \/>\n<a href=\"https:\/\/handlebarsjs.com\/\">https:\/\/handlebarsjs.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using the Handlebars.js templating system, a common need is to be able to iterate through an array of JavaScript objects in order to build an HTML list. This is useful for &#8216;Atomic&#8217; web programming, where a component is defined, and you may have a dynamic number of such components in your database. For example, &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/10\/17\/handlebars-how-to-iterate-through-an-array-of-objects\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Handlebars: How to iterate through an array of objects<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179,44],"tags":[180,45],"class_list":["post-2402","post","type-post","status-publish","format-standard","hentry","category-handlebars","category-javascript-language","tag-handlebars-js","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2402"}],"version-history":[{"count":7,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2402\/revisions"}],"predecessor-version":[{"id":2411,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2402\/revisions\/2411"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}