<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aman Shrivastava on Medium]]></title>
        <description><![CDATA[Stories by Aman Shrivastava on Medium]]></description>
        <link>https://medium.com/@amanshrivastava_21654?source=rss-a84a9b970fbe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6MZZOjZodoFYX4fxEkydrA.jpeg</url>
            <title>Stories by Aman Shrivastava on Medium</title>
            <link>https://medium.com/@amanshrivastava_21654?source=rss-a84a9b970fbe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 02:58:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amanshrivastava_21654/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Array Methods Javascript]]></title>
            <link>https://medium.com/@amanshrivastava_21654/array-methods-javascript-4e64dbe41bb3?source=rss-a84a9b970fbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e64dbe41bb3</guid>
            <category><![CDATA[happy]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Aman Shrivastava]]></dc:creator>
            <pubDate>Thu, 28 Jul 2022 16:35:07 GMT</pubDate>
            <atom:updated>2022-07-28T16:53:41.653Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mMrRHyhQ1VK3O9mD" /></figure><p>Arrays are javascript data types that can store multiple values. Arrays simplify how data is stored by storing many values in a single variable. Here are some characteristics that distinguish arrays from other data types:</p><ul><li>Arrays are identified with their square brackets []</li><li>Can Store different types of data types including strings, numbers, booleans, objects e.t.c.</li><li>Arrays are ordered and indexed from zero 0</li><li>Arrays are simple/one-dimensional (has only one level) or complex/multidimensional (nests one or more arrays)</li><li>Arrays are mutable or modifiable meaning their values can be manipulated or changed using various methods.</li><li>Array values can be accessed using the bracket notation []</li></ul><p>In this article, I compile some popular inbuilt array methods used to manipulate data in arrays.</p><h3>1. Changing elements in an array</h3><p>Since arrays are ordered from zero [0] onwards, their values are accessed using indexes. This means you can add or remove elements easily by referencing their indexes. Like this;</p><pre>const countries = [&quot;Kenya&quot; , &quot;Uganda&quot;,&quot;Tanzania&quot; ]<br>countries[1]=&#39;Morocco&#39;<br>console.log(countries )  //returns [&quot;Kenya&quot; , &quot;Morroco&quot;, &quot;Tanzania&quot;]</pre><h3>2. Find the length of an array</h3><p>Use the length()method to find out the size of the array</p><pre>const countries = [&quot;Kenya&quot; , &quot;Uganda&quot;,&quot;Tanzania&quot; ]<br>console.log(countries.length) // -&gt; 3 is the size of the array</pre><h3>3. Join arrays</h3><p>Use the concat()method to join two arrays together like this;</p><pre>const firstList = [1, 2, 3]<br>const secondList = [4, 5, 6]<br>const newList = firstList.concat(secondList)</pre><pre>console.log(newList) //[1,2,3,4,5,6]</pre><h3>4. Check if an element exists in the Array</h3><p>Use the indexOf()to check whether an item exists in an array. If it does exist, it returns the index else it returns -1.</p><pre>Let&#39;s use our list of countries again<br>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;,&quot;Tanzania&quot; ]</pre><pre>countries.indexOf(&#39;Tanzania&#39;)  //returns 2<br>countries.indexOf(&#39;Rwanda&#39;)   //t=returns -1</pre><h3>5. Convert array to string</h3><p>Use the toString()method to change an array into a string.</p><pre>Let&#39;s use our list of countries <br>const countries = [&quot;Kenya&quot; , &quot;Uganda&quot;, &quot;Tanzania&quot; ]</pre><pre>console.log(countries.toString()) // ( Kenya,Uganda,Tanzania)</pre><p>Alternatively, you can use the join() method to specify how elements should appear on the string.</p><pre>console.log(countries.join()) // KenyaUgandaTanzania -bundled together<br>console.log(countries.join(&#39;&#39;)) //KenyaUgandaTanzania-joined in one string<br>console.log(countries.join(&#39; &#39;)) //Kenya Uganda Tanzania-separate strings in a string<br>console.log(countries.join(&#39;, &#39;)) //Kenya,Uganda,Tanzania-separated by commas<br>console.log(countries.join(&#39; # &#39;)) //Kenya# Uganda# Tanzania-separated by hashtags</pre><h3>6. Add elements into an array</h3><p>Add items to the end of an array using the push method.</p><pre>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot; ]<br>countries.push(&quot;Somalia&quot;)</pre><pre>//returns </pre><pre>const countries=[&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;,&quot;Somalia&quot; ]</pre><p>You can choose to create a new array with extra items like this;</p><pre>const countries=[&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;]<br>let newCountries=countries.push(&quot;Ukraine&quot;)</pre><pre>//returns </pre><pre>newCountries=[&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;,&quot;Ukraine&quot;]</pre><h3>7. Remove elements from the start of an array</h3><p>shift() is similar to pop() , the only difference is it removes the first element of the array. This shifts all the other elements to a lower index.</p><pre>const countries=[&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;]<br>countries.shift()</pre><pre>console.log(countries)  //returns [&quot;Uganda&quot;, &quot;Tanzania&quot; ]</pre><p><em>Notice, that the first element has been removed.</em></p><p>The unshift()method does the opposite. unshift() will add items to the beginning of an array, changing the index of the other elements to a higher index.</p><h3>8. Remove items from the end of an array</h3><p>Use the pop() method to remove elements from the end of an array.</p><pre>const countries=[&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;]<br>countries.pop()</pre><pre>console.log(countries) //returns [&quot;Kenya&quot;, &quot;Uganda&quot;]</pre><p><em>Notice Tanzania has been removed from the array</em></p><h3>9. Slice array items</h3><p>The slice() method is used to extract elements from an array to create a new array. It takes two parameters, the start index and the end index(this position is however not picked but rather the previous one before it) Like this;</p><p>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;,&quot;Somalia&quot; ]<br>let newCountries=countries.slice(1,3)</p><p>console.log(newCountries) // returns [&quot;Uganda&quot;,&quot;Tanzania&quot;]</p><p>Notice that the 2nd index was extracted yet the 3rd index had been specified.</p><h3>10. Remove and replace items in the array</h3><p>Use the splice()method to remove and replace items at any index in an array.Not just the start or end of an array as the shift() and pop()</p><p>The splice method takes 3 parameters where to add or remove an item, how many items to be removed, and what items to add to the array</p><p><strong>Clear an array ;</strong></p><pre>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;,&quot;Somalia&quot; ]<br>countries.splice()<br>console.log(countries)  //returns empty list []</pre><p>Remove an item at a specified index.</p><p>Here it takes two parameters the index of items to be removed and the number of items to be removed.</p><pre>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;,&quot;Somalia&quot; ]<br>countries.splice(1,1)<br>console.log(countries)  //returns  [ &quot;Kenya&quot;, &quot;Tanzania&quot;,&quot;Somalia&quot;]</pre><p><strong>Add items at a specific index</strong></p><pre>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;,&quot;Somalia&quot; ]<br>countries.splice(2,0,&quot;Peru&quot;,&quot;Austria&quot;)</pre><pre>console.log(countries)  //returns [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Peru&quot;,&quot;Austria&quot;,&quot;Tanzania&quot;,&quot;Somalia&quot; ]</pre><h3>11. Copy an array</h3><p>The spread operator ...allows us to duplicate an array to make another array like this;</p><pre>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;]<br>const newCountries =[...countries] </pre><pre>console.log(newCountries) </pre><pre>// returns [[&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot; ],[&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot; ]]</pre><p>The spread operator can join two arrays together.</p><p><strong>join arrays</strong></p><pre>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;]<br>const newCountries=[&quot;China&quot;, &quot;Yugoslavia&quot;,...countries ,&quot;Djibouti&quot;]</pre><pre>console.log(newCountries)  <br>//returns [&quot;China&quot;, &quot;Yugoslavia&quot;, &quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;,&quot;Djibouti&quot;]</pre><h3>12. Sort and Array</h3><p>The sort() method arranges an array alphabetically</p><pre>const countries = [&quot;Kenya&quot;, &quot;Uganda&quot;, &quot;Tanzania&quot;]<br>countrie.sort()</pre><pre>console.log( countries ) //returns [&quot;Kenya&quot;, &quot;Tanzania&quot;,&quot;Uganda&quot; ]</pre><p>Notice Tanzania moves to index one since it comes before Uganda on the alphabet.</p><h3>13. Reverse an array</h3><p>The reverse() method changes the sequence in a given array, such that the first element becomes the last and the last becomes the first.</p><p>You can use it to sort an array in descending order:</p><pre>const countries = [&quot;Kenya&quot;, &quot;Tanzania&quot;,&quot;Uganda&quot; ]<br>countries.reverse()</pre><pre>console.log(countries) //returns [&quot;Uganda&quot;, &quot;Tanzania&quot;,&quot;Kenya&quot;]</pre><h3>14.Find the max and minimum values in an array</h3><p>There are many ways to find the maximum and minimum of elements in an array. Using the spread operator ... is what I found to be the easiest.</p><p><strong>Find maximum</strong></p><pre>const array1 = [1, 3, 2];</pre><pre>console.log(Math.max(...array1));  //returns 3</pre><p><strong>Find minimum</strong></p><pre>const array1 = [2, 3, 1];</pre><pre>console.log(Math.min(...array1));  //returns 1</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e64dbe41bb3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>