{"id":2121,"date":"2019-06-09T06:23:21","date_gmt":"2019-06-09T04:23:21","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2121"},"modified":"2019-06-09T11:15:37","modified_gmt":"2019-06-09T09:15:37","slug":"iterate-elements-of-an-array-in-python-and-javascript","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/iterate-elements-of-an-array-in-python-and-javascript\/","title":{"rendered":"Iterate elements of an array in Python and Javascript"},"content":{"rendered":"<p>We are going to make a comparison among two ways to iterate through elements in Python and Javascript.<\/p>\n<h2>Iterate in Python<\/h2>\n<p>Python has the most clean code to iterate through element of an array (a list, in Python&#8217;s language).<\/p>\n<pre class=\"lang:default decode:true\">a = [1,2,3]\r\n\r\nfor element in a:\r\n    print(element)<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:default decode:true\">1\r\n2\r\n3\r\n&gt;&gt;&gt;<\/pre>\n<h2>Iterate in javascript<\/h2>\n<p>Javascript has the forEach method to iterate the elements.<\/p>\n<pre class=\"lang:default decode:true \">a.forEach(function(element){\r\n   console.log(element); \r\n});\r\n1\r\n2\r\n3<\/pre>\n<h2>Iterate in javascript (arrow function)<\/h2>\n<p>You can also use the arrow function like this (it&#8217;s the same thing as above):<\/p>\n<pre class=\"lang:default decode:true\">a.forEach(element =&gt; {\r\n  console.log(element);  \r\n});\r\n1\r\n2\r\n3<\/pre>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" title=\"Iterate elements of an array in Python and Javascript\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/SRivwsf-BXA?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2>Even more similar<\/h2>\n<p>You can also go in a loop in something more close to Python&#8217;s language in javascript.<\/p>\n<h3>Getting the index out of it<\/h3>\n<p>When you use in in the for loop, you will have the index number of each element.<\/p>\n<pre class=\"lang:default decode:true\">let arr = [1,2,3];\r\n\r\nfor (let index in arr){\r\n console.log(index)\r\n}<\/pre>\n<p>This will just give you the index of the element, not the element itself.<\/p>\n<p>Output:<\/p>\n<pre class=\"lang:default decode:true \">0\r\n1\r\n2<\/pre>\n<h3>Getting the item out of an array with &#8216;in&#8217;<\/h3>\n<p>If you want the element with the &#8216;in&#8217;, you can look into the arr[index] to get the element.<\/p>\n<pre class=\"lang:default decode:true \">let arr = [1,2,3];\r\n\r\nfor (let index in arr){\r\n console.log(arr[index])\r\n}<\/pre>\n<p>Output<\/p>\n<pre class=\"lang:default decode:true \">1\r\n2\r\n3<\/pre>\n<h3>Getting the item directly with &#8216;of&#8217;<\/h3>\n<p>Using &#8216;of&#8217;, you got the element without having to use the index.<\/p>\n<pre class=\"lang:default decode:true \">let arr = [1,2,3];\r\n\r\nfor (let item of arr){\r\n console.log(item)\r\n}<\/pre>\n<p>Output<\/p>\n<pre class=\"lang:default decode:true \">1\r\n2\r\n3<\/pre>\n<p><iframe loading=\"lazy\" title=\"Iterate elements of an array in Python and Javascript 3\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/Y2jkqSdHLaA?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2>The classic for loop<\/h2>\n<p>Finally we have the classic way to make a loop through the items of an array in javascript, that is more complex to write, compared to the other ways.<\/p>\n<pre class=\"lang:default decode:true \">for (i=0; i&lt;a.length; i++){\r\n    console.log(a[i])}<\/pre>\n<p>Output<\/p>\n<pre class=\"lang:default decode:true \">1\r\n2\r\n3<\/pre>\n<p><iframe loading=\"lazy\" title=\"Iterate elements of an array in Python and Javascript 3\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/Y2jkqSdHLaA?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"How to iterate elements in an array or a list in Python and Javascript.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/iterate-elements-of-an-array-in-python-and-javascript\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":382,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,334],"tags":[278,415,414,233,60,4],"class_list":["post-2121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-javascript-and-python","tag-array","tag-elements","tag-iterate","tag-javascript","tag-list","tag-python"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=2121"}],"version-history":[{"count":8,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2121\/revisions"}],"predecessor-version":[{"id":2132,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2121\/revisions\/2132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/382"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}