{"id":1407,"date":"2018-06-09T11:50:38","date_gmt":"2018-06-09T11:50:38","guid":{"rendered":"https:\/\/tinjurewp.com\/jsblog\/?p=1407"},"modified":"2018-06-09T11:50:38","modified_gmt":"2018-06-09T11:50:38","slug":"learning-javascript-array-methods","status":"publish","type":"post","link":"https:\/\/learncode.tinjurewp.com\/learning-javascript-array-methods\/","title":{"rendered":"Learning JavaScript Array Methods"},"content":{"rendered":"<p class=\"note icon-note\"><strong>Note<\/strong>: This post is work-in-progress learning-note and still in active development and updated regularly.<\/p>\n<p>In previous learning-note post,\u00a0<a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-javascript-arrays\/\" target=\"_blank\" rel=\"noopener\">Understanding JavaScript Arrays<\/a> some basic features of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\" target=\"_blank\" rel=\"noopener\">JavaScript arrays<\/a> : syntax, creating an array, accessing array elements, recognizing an array, looping through an array elements and basic adding\/deleting elements were discussed.<\/p>\n<p>From JS data type &amp; structure perspective, JS <code>Arrays<\/code> can be compared with &#8216;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\" target=\"_blank\" rel=\"noopener\">String<\/a>&#8216; as both consist of sequence of elements that can be accessed via index number. JS <code>strings<\/code> are <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Immutable\" target=\"_blank\" rel=\"noopener\">immutable<\/a> meaning original elements of a string can&#8217;t be modified once it is created (a sub-string can be created from the original). On the other hand, JS <code>arrays<\/code> are <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Mutable\" target=\"_blank\" rel=\"noopener\">mutable<\/a> meaning original arrays (not an array copy) can be modified using array <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array#Methods\" target=\"_blank\" rel=\"noopener\">method()<\/a>.<\/p>\n<p>In this learning-note post,\u00a0we will explore array manipulation using array method(). The real strength of JS arrays are in built-in <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Indexed_collections#Array_methods\" target=\"_blank\" rel=\"noopener\">methods<\/a> and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array#Properties_2\" target=\"_blank\" rel=\"noopener\">properties<\/a> which we will discuss using simple examples. A more detail complete list of array properties &amp; methods and their use cases is available on <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\" target=\"_blank\" rel=\"noopener\">Array &#8211; MDN Documents<\/a>.<\/p>\n<p class=\"tip icon-tip\"><strong>Tip<\/strong>: In this post JS an array method is referred as <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array#Methods\" target=\"_blank\" rel=\"noopener\">method()<\/a>. Proper way to write an array is <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array#Methods_2\" target=\"_blank\" rel=\"noopener\">Array.prototype.method()<\/a>, where <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/prototype\" target=\"_blank\" rel=\"noopener\">Array.prototype<\/a> refers to the Array object itself.<\/p>\n<h3>Mutator Methods<\/h3>\n<p>These are methods that modify the original elements of an JS array. In this section we will look at methods that add,\u00a0 remove, reverse, replace, and modify elements in an array.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/isArray&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;isArray()&lt;\/a&gt;<\/code><\/strong><\/h6>\n<p><em>Returns <code>true<\/code> if a variable is an array, if not <code>false<\/code>.<\/em><\/p>\n<p>In the previous learning-note <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-javascript-arrays\/\" target=\"_blank\" rel=\"noopener\">Understanding JavaScript Arrays<\/a>, we discussed how to <strong>recognize an array<\/strong> from an object using <code>isArray()<\/code> method. Lets revisiting the same example below:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ initialize myCar array with four items \nlet myCar = [ &quot;Toyota&quot;, &quot;Corolla&quot;, 2018 ];\n\n\/\/ proof if myCar is an array\nArray.isArray(myCar); \/\/OUTPUT =&gt; true\n\n\/\/checking with typeof\ntypeof(myCar); \/\/OUTPUT =&gt; object<\/code><\/pre>\n<p><code>isArray()<\/code> is a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Boolean\" target=\"_blank\" rel=\"noopener\">Boolean<\/a> method which returns <code>true<\/code> if the value of an object variable is an array and returns <code>false<\/code> if the value of an object variable is not an array. In the example above, output of <code>isArray(myCar)<\/code> is <code>true<\/code> (line 5) confirming that <code>myCar<\/code> is an array.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/pop&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;pop()&lt;\/a&gt;<\/code><\/strong><\/h6>\n<p><em>Removes the last element from an array and returns that element.<\/em><\/p>\n<p>Lets revisit <code>cars<\/code> array that was used in the previous learning-note <a href=\"https:\/\/tinjurewp.com\/jsblog\/understanding-javascript-arrays\/\" target=\"_blank\" rel=\"noopener\">Understanding JavaScript Arrays<\/a> with four car brand names as array items.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\narr.pop();\n\n\/\/initialize cars array with four items \nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\n\/\/ pop() removes last item from an Array\ncars.pop(); \/\/ OUTPUT =&gt; GM\n\/\/access cars Array\ncars; \/\/OUTPUT =&gt; (3) [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;]<\/code><\/pre>\n<p>In the example above, <code>cars.pop()<\/code> method was applied (line 8) on the <code>cars<\/code> array (line 5) to remove the last item <code>&quot;GM&quot;<\/code> from the array. When <code>cars<\/code> array was accessed (line 10), the output confirmed our modified <code>cars<\/code> array has now only 3 items and <code>&quot;GM&quot;<\/code> was removed.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/shift&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;shift()&lt;\/a&gt;<\/code><\/strong><\/h6>\n<p><em>Removes the first element from an array and returns that element.<\/em><\/p>\n<p>Revisiting <code>cars<\/code> array example from previous section, lets remove the first element <code>&quot;Toyota&quot;<\/code> from the array using <code>shift()<\/code> method.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize cars array with four items \nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\n\/\/shift() removes first item of an Array\ncars.shift(); \/\/ OUTPUT =&gt; Toyota\n\/\/access cars Array\ncars; \/\/OUTPUT =&gt; (3) [&quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;]<\/code><\/pre>\n<p>In the example above, <code>Toyota<\/code> was removed from index <code>0<\/code> position and the remaining three items were shifted by one index number. One of the drawbacks of this method is it changes original index position of array items. If maintaining the original index number is important then <code>pop()<\/code> method is to remove an item from an array.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/push&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;push()&lt;\/a&gt;<\/code><\/strong><\/h6>\n<p><em>Adds one or more elements to the end of an array and returns the new length of the array.<\/em><\/p>\n<p>Revisiting again <code>cars<\/code> array from previous section, lets add an item with <code>&quot;BMW&quot;<\/code> value using <code>push()<\/code> method at the end <code>cars<\/code> array. The <code>push()<\/code> method takes new item(s) as parameter of the function.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize cars array with four items \nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\n\/\/ push() adds item at the end of Array\ncars.push(&quot;BMW&quot;); \/\/OUTPUT =&gt; 5\n\/\/access cars Array\ncars; \/\/OUTPUT =&gt; (5) [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;, &quot;BMW&quot;]<\/code><\/pre>\n<p>In the example above, a new value of <code>&quot;BMW&quot;<\/code> was added at the end of the array (index <code>4<\/code>) using <code>cars.push(&quot;BMW&quot;)<\/code> and now its new length is <code>5<\/code> (line 5). To add multiple values, comma ( <code>,<\/code>)separated values should be passed as function parameter (eg. <code>cars.push(&quot;Lexus&quot;, &quot;GM&quot;)<\/code> to add at the index <code>5<\/code> &amp; <code>6<\/code> position.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/shift&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;unshift()&lt;\/a&gt;<\/code><\/strong><\/h6>\n<p><em>Adds one or more elements to the front of an array and returns the new length of the array.<\/em><\/p>\n<p>In <code>cars<\/code> array below, we will use un<code>shift()<\/code> method to add <code>&quot;Lexus&quot;<\/code> at the beginning of the array. The <code>unshift()<\/code> method takes values as parameter(s) of the function.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize cars array with four items \nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\n\/\/ unshift() adds item to beginning if an Array\ncars.unshift(&quot;Lexus&quot;); \/\/ OUTPUT =&gt; 5\n\/\/access cars Array\ncars; \/\/OUTPUT =&gt; (5) [&quot;Lexus&quot;, &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;]<\/code><\/pre>\n<p>In the example above, <code>&quot;Lexus&quot;<\/code> was added with <code>cars.unshift(&quot;Lexus&quot;)<\/code> at index <code>0<\/code> position (line 7) which shifts the index position of the other items by one and increases the length of the new array. Multiple values can be added to the array passing comma-separated items as function parameters.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/splice&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;splice()&lt;\/a&gt;<\/code><\/strong><\/h6>\n<p><em>Changes the contents of an array by removing existing elements and\/or adding new elements.<br \/>\n<\/em><\/p>\n<p>The <code>splice()<\/code> method is very useful method as it can add and\/or remove one or more items from an array. If number of added items do not match with the removed items in an array then\u00a0 the final array will have different length.<\/p>\n<p><strong>Basic Syntax: <\/strong>Following is the basic syntax of <code>splice()<\/code> method.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic splice() syntax\narr.splice(index[, deleteCount, elem1, ..., elemN])\nsplice(1, 0, &quot;new&quot;);<\/code><\/pre>\n<p>The <code>splice()<\/code> method starts from the position <code>index<\/code> (with origin <code>0<\/code>): removes <code>deleteCount<\/code> elements and then inserts <code>elem1, ..., elemN<\/code> at their place. Returns the array of removed elements. The <code>splice(1, 0, &quot;new&quot;)<\/code> method (line 3) removes <code>0<\/code> items from index <code>0<\/code> and inserts <code>&quot;new&quot;<\/code> item.<\/p>\n<p>Revisiting <code>cars<\/code> array, which has four values, lets use <code>splice()<\/code> to add, remove or add &amp; remove items from its array.<\/p>\n<pre class=\"line-numbers\" data-start=\"4\"><code class=\"language-javascript\">\/\/initialize cars array with four items \nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\n\/\/ inserting an item with splice\ncars.splice(1, 0, &quot;Lexus&quot;); \n\/\/access cars\ncars;\/\/OUTPUT =&gt; (5) [&quot;Toyota&quot;, &quot;Lexus&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;]\n\n\/\/ remove one item from index 3\ncars.splice(3, 1); \/\/OUTPUT =&gt; (1) [&quot;Honda&quot;]\n\/\/access cars Array\ncars; \/\/OUTPUT =&gt; (4) [&quot;Toyota&quot;, &quot;Lexus&quot;, &quot;Ford&quot;, &quot;GM&quot;]<\/code><\/pre>\n<p>In the example above, one item <code>&quot;Lexus&quot;<\/code> was <strong>inserted<\/strong> at index <code>1<\/code> and <code>zero<\/code> item was removed (line 8). The modified <code>cars<\/code> array shows it has <code>5<\/code> arrays with <code>&quot;Lexus&quot;<\/code> at index <code>1<\/code> (line 9). Now lets <strong>remove<\/strong> one item from index 3 (line 13) and the removed item was <code>&quot;Honda&quot;<\/code> (line 13) and <code>cars<\/code> array has 4 items (line 15)<\/p>\n<p>Using <code>cars<\/code> array with five items from line 10 (above), lets <strong>remove and insert items<\/strong> from its array.<\/p>\n<pre class=\"line-numbers\" data-start=\"16\"><code class=\"language-javascript\">\/\/cars array with five items \nlet cars = [&quot;Toyota&quot;, &quot;Lexus&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;];\n\n\/\/ remove two items at index 2 replace one\ncars.splice(2, 2, &quot;BMW&quot;); \/\/OUTPUT =&gt; (2) [&quot;Ford&quot;, &quot;Honda&quot;]\n\/\/access cars array\ncars; \/\/OUTPUT =&gt; (4) [&quot;Toyota&quot;, &quot;Lexus&quot;, &quot;BMW&quot;, &quot;GM&quot;]\n\n\/\/remove items after index 2\nlet remove = cars.splice(2);\n\/\/call remove array\nremove;\/\/ OUTPUT =&gt; (2)\u00a0[&quot;BMW&quot;, &quot;GM&quot;]\n\/\/access cars array\ncars; \/\/OUTPUT =&gt; (2)\u00a0[&quot;Toyota&quot;, &quot;Lexus&quot;]<\/code><\/pre>\n<p>In the example above, the <code>cars<\/code> array has five items (line 17). Using <code>splice()<\/code> two items were <strong>removed<\/strong> starting at index 2 and <strong>replaced<\/strong> with one item <code>&quot;BMW&quot;<\/code> at index 2 (line 20). After the <code>splicing()<\/code> action, the <code>cars<\/code> array has only 4 items: <code>Toyota<\/code>, <code>Lexus<\/code>, <code>BMW<\/code>, <code>GM<\/code>.<\/p>\n<p>Lets use <code>splice()<\/code> method to <strong>remove all items<\/strong> after a defined index position. Using the <code>cars<\/code> array from line 22,\u00a0 items after index 2 were <strong>removed<\/strong> and the results were assigned to <code>remove<\/code> variable as an array (line 25) and <code>remove<\/code> array out showed that two items (<code>BMW<\/code>, <code>GM<\/code>) were removed from index <code>3-4<\/code>. The final <code>cars<\/code> array has two items <code>Toyota<\/code> and <code>Lexus<\/code> at index <code>0<\/code> and <code>1<\/code>.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/reverse&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;reverse()&lt;\/a&gt;<\/code><\/strong><\/h6>\n<p><em>Reverses the order of the elements of an array in place \u2014 the first becomes the last, and the last becomes the first.<\/em><\/p>\n<p>In our <code>cars<\/code> array we will use <code>reverse()<\/code> method to reverse the order of the items. The <code>reverse()<\/code> method does not take parameters.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize cars array with four items \nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\n\/\/ reverse items orders\ncars.reverse(); \/\/OUTPUT =&gt; (4) [&quot;GM&quot;, &quot;Honda&quot;, &quot;Ford&quot;, &quot;Toyota&quot;]<\/code><\/pre>\n<p>In the example above, <code>&quot;Toyota&quot;<\/code> is at index <code>0<\/code> and <code>&quot;GM&quot;<\/code> at index <code>3<\/code> position (line 2). After <code>reverse()<\/code> method (line 5) execution the positions of the items was reversed, <code>&quot;GM&quot;<\/code> at index <code>0<\/code> and <code>&quot;Toyota&quot;<\/code> at index <code>3<\/code> in the <code>cars<\/code> array.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/fill&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;fill()&lt;\/a&gt;<\/code><\/strong><\/h6>\n<p><em>Fills all the elements of an array from a start index to an end index with a static value.<\/em><\/p>\n<p>Revisiting <code>Cars<\/code> array example there are four items, with <code>fill()<\/code> method all the items will be replaced by the value passed as function parameter.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize cars array with four items \nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\n\/\/replaces all items\ncars.fill(&quot;Lexus&quot;); \/\/ OUTPUT =&gt; (4) [&quot;Lexus&quot;, &quot;Lexus&quot;, &quot;Lexus&quot;, &quot;Lexus&quot;]<\/code><\/pre>\n<p>In line 5, when <code>cars.fill(&quot;Lexus&quot;)<\/code> was applied all the for values of the cars array were replaced with the same <code>&quot;Lexus&quot;<\/code> value.<\/p>\n<p>The <code>fill()<\/code> method takes optional arguments as the <strong>start<\/strong> and <strong>end<\/strong> of index position as shown in the example below:<\/p>\n<pre class=\"line-numbers\" data-start=\"6\"><code class=\"language-javascript\">\/\/cars array from line 2(above)\nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\n\/\/fill selected items\ncars.fill(&quot;Lexus&quot;, 1); \/\/ OUTPUT =&gt; [&quot;Toyota&quot;, &quot;Lexus&quot;, &quot;Lexus&quot;, &quot;Lexus&quot;]\ncars.fill(&quot;Lexus&quot;, 1, 3); \/\/ OUTPUT =&gt; [&quot;Toyota&quot;, &quot;Lexus&quot;, &quot;Lexus&quot;, &quot;GM&quot;]<\/code><\/pre>\n<p>In line 10, when parameters to fill (<code>&quot;Lexus&quot;<\/code>) and <em>start<\/em> position <code>1<\/code> were supplied without <em>end<\/em> position, all items starting index <code>1<\/code> were filled with supplied fill value (<code>&quot;Lexus&quot;<\/code>). When end value was added (line 11) only items starting at index <code>1<\/code> and ending at index <code>3<\/code> were filled. Additional examples are available in the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/fill\" target=\"_blank\" rel=\"noopener\">MDN Documentation<\/a>.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/sort&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;strong&gt;sort()&lt;\/strong&gt;&lt;\/a&gt;<\/code><\/strong>:<\/h6>\n<p><em>Sorts the elements of an array in place and returns the array.<\/em><\/p>\n<p>Array <code>sort()<\/code> method sorts array items as <code>&quot;string&quot;<\/code> by default. All elements, irrespective of integer or strings, they are converted to strings and compared in lexicographic (alphabetical) order (for example, 2 being greater that 15).<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize cars array with four items \nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot; ];\n\/\/ default sort()\ncars.sort(); \/\/OUTPUT =&gt; [&quot;Ford&quot;, &quot;GM&quot;, &quot;Honda&quot;, &quot;Toyota&quot;]\n\n\/\/lcars array with lowercase items \nlet lcars = [ &quot;toyota&quot;, &quot;ford&quot;, &quot;honda&quot;, &quot;gm&quot; ];\n\/\/sort() lowercase string\nlcars.sort(); \/\/OUTPUT =&gt; [&quot;ford&quot;, &quot;gm&quot;, &quot;honda&quot;, &quot;toyota&quot;]<\/code><\/pre>\n<p>By default, <code>sort()<\/code> method applies sorting alphabetically that are either <em>uppercas<\/em>e (line 4) or <em>lowercase<\/em> (line 9).<\/p>\n<pre class=\"line-numbers\" data-start=\"10\"><code class=\"language-javascript\">\/\/ re-initialize cars array (Honda starts with lowercase)\nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;honda&quot;, &quot;GM&quot; ]; \n\n\/\/sort mixed first letter\ncars.sort();\/\/ OUTPUT =&gt; (4) [&quot;Ford&quot;, &quot;GM&quot;, &quot;Toyota&quot;, &quot;honda&quot;]<\/code><\/pre>\n<p>In the example above, when array string items are mixed (<code>&quot;honda&quot;<\/code> starts with lowercase), items with uppercase come before the lowercase item (line 14).<\/p>\n<pre class=\"line-numbers\" data-start=\"15\"><code class=\"language-javascript\">\/\/ Initialize cars array to include number\nlet myCar = [&quot;Toyota&quot;, &quot;Corolla&quot;, 2018, &quot;2 Engine&quot;];\n\n\/\/sorting with number &amp; strings\nmyCar.sort(); \/\/OUTPUT =&gt; (4) [&quot;2 Engine&quot;, 2018, &quot;Corolla&quot;, &quot;Toyota&quot;]<\/code><\/pre>\n<p>In the example above <code>myCar<\/code> array is modified to include strings and number and <code>sort()<\/code> output shows that number comes before upper and lowercases (line 19)<\/p>\n<p><strong>Sorting of Numbers<\/strong><br \/>\nThe JS<code> sort(<\/code>) method by default sorts values as <code>strings<\/code>. Sorting string values such as Toyota, Honda, Lexus in alphabetical order (Honda comes before Lexus) makes sense. However, when numbers are sorted as strings then &#8220;20&#8221; comes before &#8220;100&#8221; which returns incorrect results and practically its not helpful.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize myNum array\nlet myNum = [ 1, 2, 15, 40, 8, 100 ];\n\/\/default sort()\nmyNum.sort();\/\/OUTPUT =&gt; [1, 100, 15, 2, 40, 8]<\/code><\/pre>\n<p>In the example above, 100 comes before 15 or 2 because the stings are order in lexicographic (alphabetic) order where 1 &lt; 2 and 100 &lt; 15 which does not make much sense. This type of numeric sorting problem could be fixed by using a compare function.<\/p>\n<p><strong>The Compare Function<\/strong><br \/>\nThe following function compares two numerical values , it sends the values to the compare function and sorts the values in ascending order (except <code>NaN<\/code> &amp; <code>infinity<\/code>).<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/basic syntax (ascending\/descending order)\nfunction name(a, b) {\n  return a - b; \/\/ ascending order\n \/\/ return b - a;  \/\/ descending order\n}<\/code><\/pre>\n<p>In the above syntax the compare function subtract <code>b<\/code> from <code>a<\/code> for <em>ascending<\/em> order (line 3). Similarly, if <code>a<\/code> is subtracted from <code>b<\/code>, the compare function\u00a0 perform sort in <em>descending<\/em> order.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize myNum array\nlet myNum = [ 1, 2, 15, 40, 8, 100 ];\n\n\/\/sort ascending\nmyNum.sort(function name(a, b) {\n     return a - b; \/\/ ascending order\n});\n\/\/access myNum\nmyNum; \/\/OUTPUT =&gt; [1, 2, 8, 15, 40, 100]\n\n\/\/sort descending order\nmyNum.sort(function name(a, b) {\n     return b - a; \/\/ descending\n});\n\/\/access myNum\nmyNum; \/\/OUTPUT =&gt;[100, 40, 15, 8, 2, 1]<\/code><\/pre>\n<p>In the example above, <code>myNum<\/code> array is initialized with six numerical items in the array (line 2). To sort the values of <code>myNum<\/code> array in ascending order the compare function created previously has to passed as argument to <code>sort()<\/code> function. When applied as shown (line 4-7), the <code>myNum<\/code> array values were sorted by size or in ascending order (line 9). An example to sort in descending number is shown in lines 11-15.<\/p>\n<p>Alternately, a comparison function can be created first and the function is passed as argument to sort numerical values of an array variable. A re-write of above ascending order <code>sort()<\/code> function (line 4-7) as a comparison arrow function is shown below:<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize myNum array\nlet myNum = [ 1, 2, 15, 40, 8, 100 ];\n\n\/\/initialize sort number (ascending)\nconst numAscend = (a, b) =&gt; {\n     return a - b;\n}\n\/\/sort myNum\nmyNum.sort(numAscend); \/\/OUTPUT =&gt; [1, 2, 8, 15, 40, 100]<\/code><\/pre>\n<p>A comparison arrow function to sort numbers by size (ascending order) was initialized and assigned to <code>numAscend<\/code> <code>const<\/code> (lines 5-7). When the <code>numAscend<\/code> function was passed as argument of <code>myNum.sort()<\/code>, we got the same output (line 9). This method is much shorter and easy to read as well.<\/p>\n<p>Additional use case examples of <code>array.sort()<\/code> are available in the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/sort#Examples\" target=\"_blank\" rel=\"noopener\">MDN Documentation<\/a>.<\/p>\n<h3>Accessor Methods<\/h3>\n<p>Unlike the mutator methods, the accessor methods do not modify the original elements of an array but return a copy or representation instance. In this section, we will examine some of these methods to concatenate arrays, convert arrays to strings, copy portions of an array to a new array and find the indices of arrays.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/concat&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;concat()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>Returns a new array comprised of this array joined with other array(s) and\/or value(s).<\/em><\/p>\n<p>The <code>concat()<\/code> method merges two or more arrays together into a new array. It accepts any number of arguments &#8211; either arrays or values (line 2).<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/basic concat syntax\narr.concat(arg1, arg2...)\n\n\/\/ initialize cars, models &amp; year arrays\nlet cars = [ &quot;Toyota&quot;, &quot;Honda&quot;, &quot;Ford&quot;];\nlet models = [ &quot;Camry&quot;, &quot;Civic&quot;, &quot;Fusion&quot; ];\nlet year = [2018];\n\n\/\/ concatinate into myCars array (2 arrays)\nlet myCars = cars.concat(models);\n\/\/access myCars array\nmyCars;\/\/OUTPUT =&gt;(6) [&quot;Toyota&quot;, &quot;Honda&quot;, &quot;Ford&quot;, &quot;Camry&quot;, &quot;Civic&quot;, &quot;Fusion&quot;]\n\n\/\/concat 3 arrays into myNewCars\nlet myNewCars = cars.concat(models, year);\n\/\/access myNewCars array\nmyNewCars; \n\/\/OUTPUT\n(7)\u00a0[&quot;Toyota&quot;, &quot;Honda&quot;, &quot;Ford&quot;, &quot;Camry&quot;, &quot;Civic&quot;, &quot;Fusion&quot;, 2018]<\/code><\/pre>\n<p>In the example above, three arrays <code>cars<\/code>, <code>models<\/code> and <code>year<\/code> are initialized (Lines 5-7). In line 10, <code>myCar<\/code> array is concatenated with <code>models<\/code> array and the merged array is assigned to <code>myCars<\/code> array. The new <code>myCar<\/code> array outputs combination of two arrays. Similarly, in line 15, two arrays <code>models<\/code> and <code>year<\/code> are merged with <code>cars<\/code> array and assigned the output to <code>myNewCars<\/code> array (lines 15). The <code>myNewCars<\/code> array merges all three arrays into one single array. Using <code>concat()<\/code> method, multiple arrays can be merged into a single new array.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/join&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;join()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>Joins all elements of an array into a string and returns a new string.<\/em><\/p>\n<p>The <code>join()<\/code> method joins one or more arrays into a new string. The elements are separated, by default, by a comma ( <code>,<\/code> )\u00a0 without any character between them.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\narr.join([separator])\n\n\/\/ initialize cars array\nlet cars = [ &quot;Toyota&quot;, &quot;Honda&quot;, &quot;Ford&quot;];\n\n\/\/join the items into a string\nlet carString = cars.join();\n\/\/access carString array\ncarString; \/\/ OUTPUT =&gt; &quot;Toyota,Honda,Ford&quot;<\/code><\/pre>\n<p>In the example above, <code>cars<\/code> array items were join with <code>join()<\/code> method without any arguments (default), and values were assigned to a new <code>carString<\/code> array (line 8) and its output was comma-separated string (line 10).<\/p>\n<pre class=\"line-numbers\" data-start=\"11\"><code class=\"language-javascript\">\/\/join array items into a string with parameter \nlet carString = cars.join(&#039;, &#039;);\nlet carString1 = cars.join(&#039; + &#039;);\nlet carString2 = cars.join(&#039;&#039;);\n\n\/\/access carString, carString1 &amp; carString2\ncarString; \/\/ OUTPUT =&gt; &quot;Toyota, Honda, Ford&quot;\ncarString1; \/\/ OUTPUT =&gt; &quot;Toyota + Honda + Ford&quot;\ncarString2; \/\/ OUTPUT =&gt; &quot;ToyotaHondaFord&quot;<\/code><\/pre>\n<p>In the example above, using the same <code>cars<\/code> array (line 5) the elements were joined with comma and a space (line 12), then plus and a space (line 13) and with a empty string (line 14). With added parameters (lines 17-18) the joined elements can be formatted into more readable string. An empty string argument eliminates comma completely (line 19).<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/slice&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;slice()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>The slice() method returns copy of a portion of an array into a new array object selected from <code>begin<\/code> to <code>end<\/code> (<code>end<\/code> not included). The original array will not be modified..<\/em><\/p>\n<p>The <code>slice()<\/code> method syntax <strong>begins<\/strong> with zero-based index at which to begin extraction. If unspecified slice <strong>begins<\/strong> from index <code>0<\/code>. For the <strong>end<\/strong> point of slice extraction it extracts up to but NOT including end.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\narr.slice([begin[, end]])\n\n\/\/initialize cars array with four items\nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;, &quot;BMW&quot; ];\n\n\/\/ slice cars array 2 to 5\nlet myCars = cars.slice(2, 4);\n\/\/ access myCars;\nmyCars; \/\/ OUTPUT =&gt; (2) [&quot;Honda&quot;, &quot;GM&quot;]\n\n\/\/slice a new array from 2 to end of Array\nlet myCars = cars.slice(2);\n\/\/access myCars new Array\nmyCars; \/\/OUTPUT =&gt; (3) [&quot;Honda&quot;, &quot;GM&quot;, &quot;BMW&quot;]<\/code><\/pre>\n<p>In the example above, <code>slice()<\/code> creates a new array <code>myCars<\/code> from <code>cars<\/code> (line 8) from the elements beginning at index <code>2<\/code> through the <code>fifth<\/code> element but not including <code>5<\/code> (indexing from <code>2<\/code> to <code>4<\/code>) as shown in <code>myCars<\/code> output (line 10). If end to extract is omitted (line 13) slice extracts through the end of array (line 15) indexing three elements beginning at index <code>2<\/code>.<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/indexOf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;indexOf()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.<\/em><\/p>\n<p>The <code>indexOf()<\/code> method is used to locate the value of selected element(s) in an array. It <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/indexOf#Description\" target=\"_blank\" rel=\"noopener\">compares<\/a> <code>searchElement<\/code> to elements of the array using <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Comparison_Operators#Using_the_Equality_Operators\">strict equality<\/a> (the same method used by the <code>===<\/code>\u00a0or triple-equals\u00a0operator). The <code>fromIndex<\/code> in the syntax refers to the index to start the search at (default is <code>0<\/code> &amp; entire array is searched).<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\narr.indexOf(searchElement[, fromIndex])\n\n\/\/initialize cars array with four items\nlet cars = [ &quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;, &quot;BMW&quot; ];\n\n\/\/find 2 index of an array item\ncars.indexOf(&quot;Honda&quot;); \/\/OUTPUT =&gt; 2\n\n\/\/indexing non-existing item\ncars.indexOf(&quot;Lexus&quot;); \/\/OUTPUT =&gt; -1<\/code><\/pre>\n<p>In the example above, <code>indexOf()<\/code> method is used to search the index value in <code>Cars<\/code> array (line 5). Using <code>indexOf()<\/code> in <code>cars<\/code> array index value of <code>Honda<\/code> was determined as 2 (line 8). If an non-existing item\u00a0 (eg. <code>Lexus<\/code>) is searched, index value of <code>-1<\/code> (refers not found) is returned (line 11).<\/p>\n<p class=\"alert icon-alert\">One of the drawbacks of the <code>indexOf()<\/code> method is that value of <code>NaN<\/code> can&#8217;t be found with index of in an array.<\/p>\n<p>To quote from <a href=\"http:\/\/2ality.com\/2013\/12\/array-prototype-find.html\" target=\"_blank\" rel=\"noopener\">2ality<\/a> &#8221; <em>It\u2019s a well-known limitation of <code>Array.prototype.indexOf()<\/code> that it can\u2019t find <code>NaN<\/code>, because it searches for elements via <code>=== [1]<\/code><\/em>. Below we will look few examples to overcome this problem.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize carsN array with NaN element\nlet carsN = [&quot;Toyota&quot;, &quot;Ford&quot;, NaN, &quot;GM&quot;];\n\n\/\/access with indexOf\ncarsN.indexOf(NaN); \/\/OUTPUT =&gt; -1\n\/\/ access using includes()\ncarsN.includes(NaN); \/\/OUTPUT =&gt; true<\/code><\/pre>\n<p>In the example above, the <code>indexOf()<\/code> method, a value of <code>-1<\/code> is returned (line 5) which refers to &#8211; <em>not found<\/em>. But this is wrong value because <code>NaN<\/code> is index at <code>2<\/code> (line 2) as <code>===<\/code> equality doesn&#8217;t work for <code>NaN<\/code>.\u00a0 Array <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/includes\" target=\"_blank\" rel=\"noopener\"><code>includes()<\/code> method<\/a>, check inclusion of element in array (line 7) but only returns <code>true<\/code> ( if found) and <code>false<\/code> (if not found).<\/p>\n<p>In <a href=\"http:\/\/2ality.com\/2013\/12\/array-prototype-find.html\" target=\"_blank\" rel=\"noopener\">2ality<\/a> blog post, <a href=\"http:\/\/rauschma.de\/\" target=\"_blank\" rel=\"noopener\">Dr. Axel Rauschmayer<\/a> suggest creating a general <code>elemIs()<\/code> function to overcome this problem as shown below:<\/p>\n<pre class=\"line-numbers\" data-start=\"8\"><code class=\"language-javascript\">\/\/initialize carsN array with NaN element\nlet carsN = [&quot;Toyota&quot;, &quot;Ford&quot;, NaN, &quot;GM&quot;];\n\n\/\/initialize a general function\nfunction elemIs(x) {\n  return Object.is.bind(null, x);\n}\n\/\/access using elemIs() function\ncarsN.findIndex(elemIs(NaN));\/\/OUTPUT =&gt; 2<\/code><\/pre>\n<p>In the example above, a simple function <code>elemIs()<\/code> was created (lines 5-7). Using <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/findIndex&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;array.findIndex()&lt;\/a&gt;<\/code> method with <code>elemIs()<\/code> function as its parameter, position of <code>NaN<\/code> element can be correctly located (line 16).<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/lastIndexOf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;lastIndexOf()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found. The array is searched backwards, starting at <code>fromIndex<\/code>.<\/em><\/p>\n<p>Basic syntax of the <code>lastIndexOf()<\/code> method is shown below (line 2) where <code>searchElement<\/code> refers to element to locate in the array, and <code>fromIndex<\/code> is optional.<\/p>\n<p>Just like in <code>indexOf()<\/code> method, the <code>lastIndexOf<\/code> compares <code>searchElement<\/code> to elements of the array using <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Comparison_Operators#Using_the_Equality_Operators\">strict equality<\/a> (the same method used by the ===, or triple-equals, operator).<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\narr.lastIndexOf(searchElement, fromIndex)\n\n\/\/initialize cars array with four items\nlet cars = [ &quot;Toyota&quot;, &quot;BMW&quot;, &quot;Honda&quot;, &quot;GM&quot;, &quot;BMW&quot; ];\n\n\/\/ find last instance of an array item\ncars.lastIndexOf(&quot;BMW&quot;); \/\/OUTPUT =&gt; 4<\/code><\/pre>\n<p>In the example above,\u00a0 when the <code>lastIndexOf()<\/code> method was applied (line 8) to check whether <code>&quot;BMW&quot;<\/code> was the last index of <code>cars<\/code> array (line 5) and its output showed that it was indeed the last index (i.e. <code>4<\/code>).<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/toString&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;toString()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>Returns a string representing the array and its elements.<\/em><\/p>\n<p>The <code>toString()<\/code> method converts an array into comma-separated array values.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/initialize cars array with four items\nlet myCar = [ &quot;Toyota&quot;, &quot;Camry&quot;, 2018 ];\n\n\/\/ convert array to string\nmyCar.toString(); \/\/OUTPUT =&gt; &quot;Toyota,Camry,2018&quot;<\/code><\/pre>\n<h3>Iteration Methods<\/h3>\n<p>The Iteration method make use of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Loops_and_iteration\" target=\"_blank\" rel=\"noopener\">JS loop &amp; iteration<\/a> and operate on every item in an array, once at a time. These methods allow to perform actions such as filtering out the desired results of an array, reduce array items down to a single value, and search through arrays to find values or indices.<\/p>\n<h5>Arrow Function &#8211; Revisit<\/h5>\n<p>To quote the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions\" target=\"_blank\" rel=\"noopener\">MDN Documentation<\/a>: <em>An <strong>arrow function expression<\/strong> has a shorter syntax than a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/function\">function expression<\/a> and does not have its own <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/this&quot;&gt;this&lt;\/a&gt;<\/code>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/arguments\">arguments<\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/super\">super<\/a>, or <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/new.target\">new.target<\/a>. These function expressions are best suited for non-method functions, and they cannot be used as constructors.<\/em> They are written as equals sign ( <code>=<\/code> ) followed by a greater than ( <code>&gt;<\/code> ) sign : <code>=&gt;<\/code> .<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/function syntax\nlet functionName = function() {\n  \/\/ code block\n}\n\/\/invoke function\nfunctionName();\n\n\/\/arrow function syntax\nlet functionName = () =&gt; {\n\/\/ code block\n}\n\/\/ function with one parameter\nlet functionName = parameter1 =&gt; {\n  \/\/code blocks\n}\n\/\/invoke function\nfunctionName();<\/code><\/pre>\n<p>A JS <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Functions\" target=\"_blank\" rel=\"noopener\">function<\/a> can be defined as a block of reusable (procedure) codes that can be executed to perform a task or calculate value. A typical <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Functions#Function_declarations\" target=\"_blank\" rel=\"noopener\">function syntax<\/a> and invoking a function is shown above (lines 2-6).<\/p>\n<p>The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions\" target=\"_blank\" rel=\"noopener\">arrow function<\/a> is represented with much <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions#Basic_Syntax\" target=\"_blank\" rel=\"noopener\">shorter syntax<\/a> and written as shown above (line 8-17). Parameters can be included in the parentheses in function (line 2-6) and arrow function (line 9). In arrow function, the parentheses (line 9) can be ommited if there is only one parameter (line 13).<\/p>\n<h6><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/forEach&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;forEach()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>The <code>&lt;strong&gt;forEach()&lt;\/strong&gt;<\/code> method executes a provided function once for each array element.<\/em><\/p>\n<p>A basic syntax of <code>forEach()<\/code> method is shown below (lines 2-4).<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\narr.forEach(function(item, index, array) {\n  \/\/ ... do something with item\n});\n\n\/\/initialize cars array\nlet cars = [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;]; \/\/\n\n\/\/ Print out each item in the array\ncars.forEach(Car =&gt; { \/\/from cars to individual car\n    console.log(Car);\n})\n\/\/OUTPUT\nToyota\nFord\nHonda\nGM<\/code><\/pre>\n<p>In the example above, <code>cars<\/code> array is initialize with four car names (line 7). Using <code>forEach()<\/code> method (lines 10-12), each item in the <code>cars<\/code> array can be as shown in the output (lines 14-17).<\/p>\n<p>An alternative method to achieve the same results is by using the <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/for&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;for&lt;\/a&gt;<\/code> loop keyword. In this method <code>for<\/code> keyword loops through the <code>length<\/code> property of the array\u00a0 as shown below (lines 19-21).<\/p>\n<pre class=\"line-numbers\" data-start=\"18\"><code class=\"language-javascript\">\/\/ Loop through the length of the array\nfor (let i = 0; i &lt; car.length; i++) {\n    console.log(car[i]);\n}\n\/\/OUTPUT\nToyota\nFord\nHonda\nGM<\/code><\/pre>\n<p>In the case of an array, <code>forEach()<\/code> is more concise and simpler iteration method.<\/p>\n<h6 id=\"array-map\"><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;map()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>Creates a new array with the results of calling a provided function on every element in this array.<\/em><\/p>\n<p>The <code>array.map()<\/code> method transforms one array into a new array after performing some operation on each item of the array. The original array is not modified and the function returns a new array value. The <code>map()<\/code> method must be assigned to a new variable.<\/p>\n<p>Lets revisit <code>cars<\/code> array and use <code>map()<\/code> to print each item, add extra character to each items and determine the length of character of each items in the array.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\nlet mapName = arr.map(function(item, index, array) {\n  \/\/ returns the new value instead of item\n})\n\n\/\/initialize cars array\nlet cars = [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;];\n\n\/\/ Print out each item in the array\nlet printCars = cars.map(car =&gt; { \/\/ individual car\n    console.log(car);\n});\n\/\/OUTPUT\nToyota\nFord\nHonda\nGM<\/code><\/pre>\n<p>In the example above, <code>map()<\/code> method iterates each item in cars array (line 7) and returns each iteration of the loop to the console (lines 14-17) one at a time until all the elements are printed. The <code>map()<\/code> method is assigned to the <code>printCars<\/code> variable.<\/p>\n<pre class=\"line-numbers\" data-start=\"18\"><code class=\"language-javascript\">\/\/add extra character X in cars array\nlet carX = cars.map(car =&gt; {\n    return `${car}X`;\n});\n\/\/access carX array\ncarX; \/\/OUTPUT =&gt; [&quot;ToyotaX&quot;, &quot;FordX&quot;, &quot;HondaX&quot;, &quot;GMX&quot;]\n\n\/\/counting length of each element\nlet cars = [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;];\n\/\/map element length\nlet carLength = cars.map(item =&gt; item.length)\nconsole.log(carLength);\/\/ OUTPUT =&gt; 6, 5, 4, 2<\/code><\/pre>\n<p>In the example above, <code>map()<\/code> is used to add an extra character (<code>X<\/code>) to each elements in the cars array (lines 19-21) and <code>carX<\/code> outputs all the elements with extra <code>X<\/code>\u00a0 (lines 23). Similarly, using <code>map()<\/code> method and <code>array.length<\/code> property, length of each elements of the <code>cars<\/code> array (line 26) could be counted and output list was assigned to <code>carLength<\/code> array (lines 28-29).<\/p>\n<h6 id=\"array-filter\"><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filter&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;filter()&lt;\/a&gt;<\/code> <\/strong>:<\/h6>\n<p><em>Creates a new array with all of the elements of this array for which the provided filtering function returns true.<\/em><\/p>\n<p>The <code>array.filter()<\/code> method calls a filtering function once for each elements in an array and returns all the elements that match the condition into a new array. If no elements in the array pass the criteria then an empty array will be return. The <code>filter()<\/code> method does not mutate (change) the original array.<\/p>\n<p>The basic syntax <code>array.filter()<\/code> method shown below (line 2-4).<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\nlet newArray = arr.filter(function(item, index, array) {\n  \/\/ should return true if the item passes the filter\n});\n\n\/\/initialize cars array\nlet cars = [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;Hyundai&quot;, &quot;BMW&quot;];\n\n\/\/ filter cars that start with &quot;H&quot; into a new list\nlet newCars = cars.filter(car =&gt; {\n  return car[0] === &quot;H&quot;;\n});\n\/\/access newCars\nnewCars; \/\/ OUTPUT =&gt; [&quot;Honda&quot;, &quot;Hyundai&quot;]<\/code><\/pre>\n<p>In the example above, <code>array.filter()<\/code> method is applied to search each elements of <code>cars<\/code> array (line 7) and filter out the list of elements with <code>&quot;H&quot;<\/code> at the index <code>0<\/code> and assign the list to <code>newCars<\/code> (lines 10-12).\u00a0 The <code>newCars<\/code>, outputs two elements <code>&quot;Honda&quot;<\/code> and <code>&quot;Hyundai&quot;<\/code> that start with <code>&quot;H&quot;<\/code> (line 14).<\/p>\n<h6 id=\"array-reduce\"><strong>Method <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/reduce&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;reduce()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>The <code>&lt;strong&gt;reduce()&lt;\/strong&gt;<\/code> method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.<\/em><\/p>\n<p>The <code>array.reduce()<\/code> method performs the callback function once for each elements in the array returns a single value. Its basic syntax is shown below (lines 2-4). The callback function parameters include:<\/p>\n<ul>\n<li><code>reducer<\/code> : the single returned value by the reduce() call back function<\/li>\n<li><code>acc<\/code> : it accumulates callback&#8217;s return value<\/li>\n<li><code>curVal<\/code> : the current element being processed<\/li>\n<li><code>curIndex<\/code> : The index position of the current element being processed (<em>optional<\/em>)<\/li>\n<li><code>arr<\/code> : the array being processed (<em>optional<\/em>)<\/li>\n<li><code>initValue<\/code> : Initial value for the first call (<em>optional<\/em>)<\/li>\n<\/ul>\n<p class=\"note icon-tip\">The <code>array.array()<\/code> method is little intricate and explained in more detail in other learning-note post <a href=\"https:\/\/tinjurewp.com\/jsblog\/learning-javascript-array-reduce-method\/\" target=\"_blank\" rel=\"noopener\">Learning JavaScript <code>Array.Reduce()<\/code> Method<\/a>.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/basic syntax\nlet reducer = arr.reduce(function(acc, curVal, curIndex, arr) {\n  \/\/ ...\n}, initValue);\n\n\/\/initialize numbers array\nlet numbers = [ 9, 56, 29, 11, 41, 18 ]; \n\n\/\/ total of all numerical values\nlet total = numbers.reduce((a, b) =&gt; {\n    return a + b;\n});\n\/\/access total\ntotal; \/\/ OUTPUT =&gt; 164\n\n\/\/initialize cars array\nlet cars = [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;Hyundai&quot;, &quot;BMW&quot;];\n\/\/ total of all numerical values\nlet reduceCars = cars.reduce((a, b) =&gt; {\n    return a + b;\n});\n\/\/access reduceCars\nredudeCars; \/\/ OUTPUT =&gt; &quot;ToyotaFordHondaHyundaiBMW&quot;<\/code><\/pre>\n<p>In the example above, <code>numbers<\/code> array is assigned with six number as array elements (line 2). The <code>array.reduce()<\/code> can be used to find the sum of all the elements in an array. Using <code>reduce()<\/code> method each elements of <code>numbers<\/code> array within the loop are added and assigned to a <code>total<\/code> variable (lines 5-7).\u00a0 The <code>total<\/code> outputs <code>164<\/code> which is sum of all the elements in <code>numbers<\/code> array.<\/p>\n<p>Likewise, <code>array.reduce()<\/code> can also be used to other data types, number, string, array etc. An example of reducing all <code>cars<\/code> array elements into a single string value is shown above (lines 11-19).<\/p>\n<p class=\"note icon-tip\"><strong>Note<\/strong>: The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/ReduceRight\" target=\"_blank\" rel=\"noopener\">array.reduceRight()<\/a> method is similar to <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/reduce&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;array.reduce()&lt;\/a&gt;<\/code> method that performs starting from right-to-left.<\/p>\n<h6><strong>Methods <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/find&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;find()&lt;\/a&gt;<\/code> &amp; <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/findIndex&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;findIndex()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>The <code>array.find()<\/code> method returns the <strong>value<\/strong> of the <strong>first element<\/strong> found in the array, if an element in the array satisfies the provided testing function or <code>undefined<\/code> if not found. On the other hand, the <code>findIndex()<\/code> returns the found <strong>index<\/strong> in the array, if an element in the array satisfies the provided testing function or <code>-1<\/code> if not found.<\/em><\/p>\n<p>Both the <code>array.find()<\/code> and <code>array.findIndex()<\/code> methods execute the function once for each element present in the array and return the <strong>value<\/strong> (in case <code>find<\/code> method) and <strong>index value<\/strong> (in case of <code>findIndex<\/code> method) of the first element in an array that pass the test. Both methods do not check the remaining values. They also do not execute the function on empty elements and do not change the original array.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/initialize carModels array\nlet carModels = [&quot;Camry&quot;, &quot;Corolla&quot;, &quot;Prius&quot;, &quot;Tundra&quot;, &quot;Tacoma&quot;];\n\n\/\/ Check if a given value is a truck\nconst isTruck = truck =&gt; {\n    return [ &quot;Trunda&quot;, &quot;Tacoma&quot; ].includes(truck);\n}\n\/\/ using arrow function\nlet isTruck = truck =&gt; truck === &quot;Tundra&quot;; \/\/ an array item\n\/\/Invoke find() \ncarModels.find(isTruck); \/\/OUTPUT =&gt; Tundra\n\n\/\/ Check if a given value is in the array\nlet isChevy = truck =&gt; truck === &quot;Silvarado&quot;; \/\/ NOT an array item\n\/\/Invoke find() \ncarModels.find(isChevy); \/\/OUTPUT =&gt; undefined<\/code><\/pre>\n<p>In the example above, <code>carModels<\/code> array is initialized with five elements (three sedans and two trucks) (line 2).\u00a0 Using a simple arrow function <code>isTruck<\/code> (line 9) to check if a value equals to <code>&quot;Tundra&quot;<\/code> (line 9) and istruck as <code>array.find()<\/code> function parameter (line 11), we can check whether it matches with each elements in <code>carModels<\/code> array. And if any element value is equal to <code>&quot;Tundra&quot;<\/code> its value is returned (line 11). If value is not matched it returns <code>&quot;undefined)<\/code> (line 16) because there is <code>&quot;Silvarado&quot;<\/code> in not in <code>carModels<\/code> array (line 2).<\/p>\n<pre class=\"line-numbers\" data-start=\"17\"><code class=\"language-javascript\">\/\/Invoke findIndex() of a listed element\ncarModels.findIndex(isTruck); \/\/OUTPUT =&gt; 3\n\/\/Invoke findIndex() of NOT listed element\ncarModels.findIndex(isChevy); \/\/OUTPUT =&gt; -1<\/code><\/pre>\n<p>Using the same example from above, when <code>isTruck<\/code> (line 9) is used with <code>findIndex()<\/code> function, it returns <code>3<\/code> instead of <code>Tundra<\/code> (line 18). Likewise, when not listed element value <code>&quot;Silavardo&quot;<\/code> is passed using <code>isChevy<\/code> as <code>findIndex()<\/code> function (line 14), it returns <code>-1<\/code> and not <code>undefined<\/code>.<\/p>\n<p class=\"note icon-tip\"><strong>Note<\/strong>: The <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/find&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;array.find()&lt;\/a&gt;<\/code> method is similar to <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/findIndex&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;array.findIndex()&lt;\/a&gt;<\/code> method which returns index value instead of the element itself.<\/p>\n<h6><strong>Methods <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/every&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;every()&lt;\/a&gt;<\/code> &amp; <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/some&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;some()&lt;\/a&gt;<\/code><\/strong> :<\/h6>\n<p><em>The <code>array.every()<\/code> method returns <code>true<\/code> if every element in this array satisfies the provided testing function. On the other hand, the <code>array.some()<\/code> method returns <code>true<\/code> if at least one element in this array satisfies the provided testing function..<\/em><\/p>\n<p>The method the <code>array.every()<\/code> method executes function once for each element in an array and returns <code>true<\/code> only when <strong>all<\/strong> elements pass the condition, otherwise returns <code>false<\/code>. Both the <code>every()<\/code> &amp; <code>some()<\/code> methods do not immutate original array and do not execute on elements without any value.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ initialize year array\nlet year = [2015, 2016, 2017, 2018];\n\n\/\/assign carYear to check an array item\nlet carYear1 = year.every(car =&gt; car &gt;= 2015);\/\/OUTPUT =&gt; true\n\/\/check item not listed in array\nlet carYear2 = year.every(car =&gt; car &gt;= 2016);\/\/OUTPUT =&gt; false<\/code><\/pre>\n<p>In the example above, using <code>every()<\/code> method we check if values in the <code>year<\/code> array (line 2) that our cars were built in 2015 or later (line 5). <code>carYear1<\/code> function pass the condition (line 5) because all the cars were built later than <code>2015<\/code> and this returns <code>true<\/code>. However,\u00a0 in the case of <code>carYear2<\/code> function returns <code>false<\/code> because there is one item listed <code>2015<\/code> fails the test though rest of the cars were built either <code>2016<\/code> and later.<\/p>\n<p>Similar to the <code>every()<\/code> method, the <code>array.some()<\/code> method also executes function once for each element in the array but returns <code>true<\/code> even if only <strong>one<\/strong> element pass the test. It returns <code>false<\/code> only when none of the element in the array pass the test.<\/p>\n<pre class=\"line-numbers\" data-start=\"8\"><code class=\"language-javascript\">\/\/check with some() if any item \nlet carYear1 = year.some(car =&gt; car &gt; 2015); \/\/OUTPUT =&gt; true\n\/\/check with some non-compliant item\nlet carYear2 = year.some(car =&gt; car &lt; 2015); \/\/OUTPUT =&gt; false<\/code><\/pre>\n<p>In the example above, using <code>some()<\/code> method we checked whether some values in the year array (line 2) are greater than <code>2015<\/code> and it passes the test on first item and returns <code>true<\/code> (line 9). But when it was\u00a0 checked whether any values were smaller than <code>2016<\/code> and yes there was one with 2015 but still it fails the test (there are values greater than 2016) and returns <code>false<\/code> (line 11).<\/p>\n<h6><strong>Methods <code>&lt;a href=&quot;https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/values&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;values()&lt;\/a&gt;<\/code> &amp; <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/keys\" target=\"_blank\" rel=\"noopener\"><code>keys()<\/code><\/a><\/strong> :<\/h6>\n<p><em>The <code>array.values()<\/code> returns a new <strong>Array Iterator<\/strong> object that contains the <strong>values<\/strong> for each index in the array. On the other hand, the <code>array.keys()<\/code> method returns a new <strong>Array Iterator<\/strong> that contains the <strong>keys<\/strong> for each index in the array.<\/em><\/p>\n<p>The basic syntax of <code>array.values()<\/code> method is shown below (line 2) and it returns a new array <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Iteration_protocols#Built-in_iterables#Built-in_iterables\" target=\"_blank\" rel=\"noopener\">iterator<\/a> object.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\narr.values();\n\n\/\/initialize carsA array\nconst carsA = [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;];\nconst carsB = carsA.values();\n\n\/\/ value iteration loop\nfor (const value of carB) {\n  console.log(value); \n}\n\/\/OUTPUT\nToyota\nFord\nHonda\nGM<\/code><\/pre>\n<p>In the above example(<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/values\" target=\"_blank\" rel=\"noopener\">adopted from the MDN documents<\/a>), <code>carsA<\/code> array is initialized with four elements (car names) (line 5). Using <code>array.values()<\/code> method <code>carsA<\/code> array elements were assigned to a new <code>carsB<\/code> array (line 6). Using a for loop, the values of each elements of <code>carsB<\/code> array (lines 9-11) can be printed in the browser console (lines 13-16).<\/p>\n<p>The basic syntax of <code>array.keys()<\/code> method is similar to <code>array.values()<\/code> discussed above (line 2) which also returns a new array <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Iteration_protocols#Built-in_iterables#Built-in_iterables\" target=\"_blank\" rel=\"noopener\">iterator<\/a> object.<\/p>\n<pre class=\"line-numbers\"><code class=\"language-javascript\">\/\/ basic syntax\narray.keys();\n\n\/\/initialize carsA array\nconst carsA = [&quot;Toyota&quot;, &quot;Ford&quot;, &quot;Honda&quot;, &quot;GM&quot;];\nconst carsB = carsA.keys();\n\n\/\/key iteration loop\nfor (let key of carsB) {\n  console.log(key); \n}\n\/\/OUTPUT\n0\n1\n2\n3<\/code><\/pre>\n<p>The example above is the same <code>carsA<\/code> array used in <code>array.values()<\/code> method. The <code>array.keys()<\/code> method operatates similarly (lines 9-11) except it returns iterator array that contains <strong>keys<\/strong> of each array (lines 13-16).<\/p>\n<p class=\"tip icon-note\"><strong>Tip<\/strong>: There are other methods and use cases listed in <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\" target=\"_blank\" rel=\"noopener\">MDN Documents<\/a> including creating <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array#Creating_a_two-dimensional_array\" target=\"_blank\" rel=\"noopener\"><strong>multi-dimensional array<\/strong><\/a>,<strong> <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array#Using_an_array_to_tabulate_a_set_of_values\" target=\"_blank\" rel=\"noopener\">tabulation of array values<\/a><\/strong> and additional use cases of methods discussed in this post.<\/p>\n<h5>Wrapping Up<\/h5>\n<p>In this post, we looked at built-in <em>mutator<\/em>, <em>accessor<\/em> and <em>iteration<\/em> methods to modify JS arrays. Using simple code snippets we modified original array elements with <em>mutator methods<\/em>, and created new array representation using <em>accessor methods<\/em> to perform concatenation, convert arrays to strings, copy portions of an array to a new array etc. Finally we explored <em>iteration method<\/em> to filtering out desired elements from original array and reducing an array to single value.<\/p>\n<p>Related Post : <a href=\"https:\/\/tinjurewp.com\/jsblog\/learning-javascript-array-reduce-method\/\" target=\"_blank\" rel=\"noopener\">Learning JavaScript <code>Array.Reduce()<\/code> Method<\/a><\/p>\n<h5>Useful Resources &amp; Links<\/h5>\n<p>While preparing this post, I have referred the following references extensively. Visit original link for additional information.<\/p>\n<ul>\n<li><a href=\"https:\/\/javascript.info\/array\" target=\"_blank\" rel=\"noopener\">Arrays<\/a> | <a href=\"https:\/\/javascript.info\/\" target=\"_blank\" rel=\"noopener\">The Modern JavaScript tutorial<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Indexed_collections\" target=\"_blank\" rel=\"noopener\">Indexed collections<\/a> | <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\" target=\"_blank\" rel=\"noopener\">The MDN JavaScript Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/understanding-arrays-in-javascript\" target=\"_blank\" rel=\"noopener\">Understanding Arrays in JavaScript<\/a> | <a href=\"https:\/\/www.digitalocean.com\/\" target=\"_blank\" rel=\"noopener\">DigitalOcean<\/a><\/li>\n<li><a href=\"https:\/\/codeburst.io\/array-functions-map-filter-18a6e5f75da1\" target=\"_blank\" rel=\"noopener\">Master Map &amp; Filter, Javascript\u2019s Most Powerful Array Functions<\/a>\u00a0 | <a href=\"https:\/\/codeburst.io\" target=\"_blank\" rel=\"noopener\">codeburst.io<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\" target=\"_blank\" rel=\"noopener\">Arrays<\/a> | The MDN Documents<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Note: This post is work-in-progress learning-note and still in active development and updated regularly. In previous learning-note post,\u00a0Understanding JavaScript Arrays some basic features of JavaScript arrays : syntax, creating an array, accessing array elements, recognizing an array, looping through an array elements and basic adding\/deleting elements were discussed. From JS data type &amp; structure perspective, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[47,4],"tags":[],"class_list":["post-1407","post","type-post","status-publish","format-standard","hentry","category-arrays","category-javascript"],"_links":{"self":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/1407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/comments?post=1407"}],"version-history":[{"count":0,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/posts\/1407\/revisions"}],"wp:attachment":[{"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/media?parent=1407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/categories?post=1407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learncode.tinjurewp.com\/wp-json\/wp\/v2\/tags?post=1407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}