<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ALAMIN JUMA MAGOTI</title>
    <description>The latest articles on DEV Community by ALAMIN JUMA MAGOTI (@alaminjuma).</description>
    <link>https://dev.to/alaminjuma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F507005%2F93ef06f8-3351-4de0-8cdc-abc25a069a97.jpeg</url>
      <title>DEV Community: ALAMIN JUMA MAGOTI</title>
      <link>https://dev.to/alaminjuma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alaminjuma"/>
    <language>en</language>
    <item>
      <title>Javascript array methods to master</title>
      <dc:creator>ALAMIN JUMA MAGOTI</dc:creator>
      <pubDate>Tue, 15 Mar 2022 10:06:20 +0000</pubDate>
      <link>https://dev.to/alaminjuma/javascript-array-methods-to-master-1kgl</link>
      <guid>https://dev.to/alaminjuma/javascript-array-methods-to-master-1kgl</guid>
      <description>&lt;p&gt;An &lt;strong&gt;array&lt;/strong&gt; is a &lt;strong&gt;type of data structure&lt;/strong&gt; that stores items that can be accessed at an index. &lt;br&gt;
For instance here is an empty array:&lt;br&gt;
&lt;code&gt;[]&lt;/code&gt;&lt;br&gt;
 and here is an array element containing different kinds of data: &lt;br&gt;
&lt;code&gt;[1, "Alamin", true, {name: 'alamin', age: 25}]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Arrays contain three kinds of methods, the &lt;strong&gt;mutator methods&lt;/strong&gt; that modify the original array, &lt;strong&gt;accessor methods&lt;/strong&gt; that returns a new value and &lt;strong&gt;iterable methods&lt;/strong&gt; which accesses each array item at a time. &lt;/p&gt;

&lt;p&gt;Here, I will focus mostly on the common array iterable methods like map(), filter(), find(), every(), some(), forEach(), reduce(). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1: map() array method&lt;/strong&gt;&lt;br&gt;
A map() methods return a new array copy of the original array which can then be used to access each array element and return its  value for each iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1,2,3,4,5];
let arrayMap = arr.map(item =&amp;gt; item += item)
console.log(arrayMap);
//[ 2, 4, 6, 8, 10 ]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code maps arr to arrayMap using a map() method and enables it to access each item , where a value is return containing an item added by itself. &lt;br&gt;
Lets say the Government increases the taxes on food items by 20 shillings, but we still need to know the previous prices.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const initialFoodPrices = [
    { image: "🍕", name: "pizza", price: 1000 },
    { image: "🍔", name: "burger", price: 800 },
    { image: "🥪", name: "sandwich", price: 600 },
   ] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if we map into initialFoodPrices, *&lt;em&gt;item *&lt;/em&gt; returns an array of objects&lt;br&gt;
&lt;code&gt;initialFoodPrices.map(item =&amp;gt;  item)&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7wxUjEOl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aynsgvzqwmcpypsxoggu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7wxUjEOl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aynsgvzqwmcpypsxoggu.PNG" alt="js image" width="659" height="505"&gt;&lt;/a&gt;&lt;br&gt;
we can access an element of each object using a .dot notation on since item returns an objects data, for example&lt;br&gt;&lt;br&gt;
&lt;code&gt;initialFoodPrices.map(item =&amp;gt;  item.price)&lt;br&gt;
//[1000, 800, 600]  &lt;br&gt;
&lt;/code&gt;will return all prices in the objects into a new array. We can then add the added taxes to the prices to return the new prices &lt;/p&gt;

&lt;p&gt;&lt;code&gt;initialFoodPrices.map(item =&amp;gt;  item.price + 20)&lt;br&gt;
(3)&amp;nbsp;[1020, 820, 620]&lt;/code&gt;&lt;br&gt;
**&lt;em&gt;Note if you don't return your value on a map(), your values will be undefined. *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2: forEach() array method&lt;/strong&gt;&lt;br&gt;
Array forEach() is used when one needs to iterate over array items one-by-one so that you can do something with them. The difference between map() and forEach(), is that map() returns a new array &lt;br&gt;
while forEach(), does not return any value.&lt;br&gt;
For example the code below, we are iterating through runners arrays and accessing every athlete to show if they run 100 meters race:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let runners = ['Kipchoge', 'Kipchumba', 'Koskei']
.forEach((athlete) =&amp;gt; {
  console.log(`${athlete} runs 100 meters`)
})
//Kipchoge runs 100 meters
//Kipchumba runs 100 meters
//Koskei runs 100 meters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;forEach() is useful when  populating data on the DOM or target an event to a group of HTML elements. Below shows how one can add an eventListner to a group of buttons with the same class/id name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="jumbotron"&amp;gt;
  &amp;lt;button class=" btn-primary text-center btns"&amp;gt;Go blue&amp;lt;/button&amp;gt;  
  &amp;lt;button class=" btn-white text-center btns"&amp;gt;Go white&amp;lt;/button&amp;gt;
  &amp;lt;button class=" btn-danger text-center btns"&amp;gt;Go red&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NN15ZIm3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4hs3ehvl77nzz3lqhpf.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NN15ZIm3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4hs3ehvl77nzz3lqhpf.PNG" alt="buttons with same class" width="708" height="155"&gt;&lt;/a&gt;&lt;br&gt;
To add an event listner to the three buttons, we will use querySelectorAll('.btns') which returns a nodeList  containing all the button elements.&lt;br&gt;
&lt;code&gt;const buttons = document.querySelectorAll('.btns')&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wXcILVHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8efkxsypgjvlfqlfexze.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wXcILVHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8efkxsypgjvlfqlfexze.PNG" alt="nodeList of buttons" width="880" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;if you eventListner directly to the grabbed elements on buttons, it will give you an error  of buttons.addEventListner is not a function. &lt;br&gt;
&lt;code&gt;buttons.addEventListener('click', () =&amp;gt; {document.body.style.backgroundColor = "#fff000"})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3RDC8BwM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fv6hzf0xda93rmc8zrv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3RDC8BwM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5fv6hzf0xda93rmc8zrv.PNG" alt="typeError" width="880" height="188"&gt;&lt;/a&gt;&lt;br&gt;
This is because we are trying to access the nodeList as one element, yet it is an array of elements(i.e the buttons). &lt;br&gt;
The nodeList exposes methods like forEach() which can help you access each button element in the nodeList and add an eventListner to  each button. Lets remove the error using forEach()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    buttons.forEach(button =&amp;gt; 
                    button.addEventListener
                    ('click', function goYellow() {

         console.log('I laugh for every click 😀😀😀😀😀😀😀')
    }))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when every button is click, an event lister of click is added to every button element in the nodeList, and a function call is executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rvam3b1R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkcmneqkovcf5odw2tc2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rvam3b1R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkcmneqkovcf5odw2tc2.PNG" alt="click" width="715" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3: filter() array method&lt;/strong&gt;&lt;br&gt;
filter() **returns **a new array  containing only the items you specified.&lt;br&gt;
for example, you may need to filter only items that are above 450 , since they are expensive from a menu list&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const availableFoods = [
    {id: "qwe234dfh", name: "Burger", image:"🍔🍔", price: 234},
    {id: "qwe2356dxh", name: "pizza", image:"🍕🍕", price: 400},
    {id: "qwe2456yh", name: "meat", image:"🍖🍖", price: 500},
    {id: "qwe2785yh", name: "chicken", image:"🍗🍗", price: 1200},
]
const expensiveItems = availableFoods.filter((items) =&amp;gt; {return items.price &amp;gt; 450})
console.log(expensiveItems)

//result
// {id: "qwe2456yh", name: "meat", image:"🍖🍖", price: 500},
// {id: "qwe2785yh", name: "chicken", image:"🍗🍗", price: 1200},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Array reduce&lt;/strong&gt;&lt;br&gt;
Array reduced is used when one needs a single value, mostly when one needs to end up with a calculated value of the whole array. it has a *&lt;em&gt;prev **and **next *&lt;/em&gt;, where prev refers to the value returned on the next iteration, and next as the current value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const reducedValues = [1, 2, 3, 4, 5].reduce((prev, next) =&amp;gt; {
    console.log(`prev: ${prev}, next: ${next}`)
    return prev + next
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KDjAuORT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ow6klfoxadjsxw8bt73c.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KDjAuORT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ow6klfoxadjsxw8bt73c.PNG" alt="reduced" width="722" height="340"&gt;&lt;/a&gt;&lt;br&gt;
Lets say we have a list of food menu and we need to calculate  the total price&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//lets calculate the tottal price of this dinner
let mydinner = [
    { id: "qwe234dfh", name: "Burger", image: "🍔🍔", price: 500 },
    { id: "qwe2456yh", name: "meat", image: "🍖🍖", price: 600 },
    { id: "qwe2785yh", name: "chicken", image: "🍗🍗", price: 400 },
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this, we need to access the prices inside an object hence we will first need to return an array containing all prices, then reduce the returned array.&lt;br&gt;
We will map into the array of objects and access the objects.prices values into an new array&lt;br&gt;
&lt;code&gt;const totalP = mydinner.map(foodPrice =&amp;gt; foodPrice.price)&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0RE-eca1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8yxmhbvk8kvn213xgza.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0RE-eca1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8yxmhbvk8kvn213xgza.PNG" alt="mapped" width="794" height="428"&gt;&lt;/a&gt;&lt;br&gt;
After that, we can then reduce the price values into a single value, totalPrice&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const totalP = mydinner.map(foodPrice =&amp;gt; foodPrice.price)
                       .reduce((prev, next) =&amp;gt; {
                                return prev + next
                                                    }, 0);
                        console.log("total bill is ..... ", totalP, "ksh")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d1OO3LNr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1liu83lu0mrx75nydvl4.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d1OO3LNr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1liu83lu0mrx75nydvl4.PNG" alt="reduce" width="800" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;some() array method&lt;/strong&gt;&lt;br&gt;
some() checks if an array item passes a particular test, hence &lt;br&gt;
returning a boolean  (true or false). For example, we can use some() to check if a certain group of students are software developers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let students = [
  {name: 'John', email: 'john@gmail.com', softwareDev: "true"},
  {name: 'John', email: 'john@gmail.com', softwareDev: "false"},
  {name: 'John', email: 'john@gmail.com', softwareDev: "true"},
]
.some((item) =&amp;gt; {return item.softwareDev})
console.log(students)
//true 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;returns true because yes some persons in the array are a softwareDev&lt;/p&gt;

&lt;p&gt;..................................................................&lt;br&gt;
Alamin Juma&lt;br&gt;
Enjoys hiking and swimming&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
