<?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>Forem: Nawaz Mujawar</title>
    <description>The latest articles on Forem by Nawaz Mujawar (@nawazmujawar).</description>
    <link>https://forem.com/nawazmujawar</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F390070%2Fc3599ba6-d60d-4658-bcf3-816e7a0a85e1.jpeg</url>
      <title>Forem: Nawaz Mujawar</title>
      <link>https://forem.com/nawazmujawar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nawazmujawar"/>
    <language>en</language>
    <item>
      <title>map, reduce, filter, sort, slice ...etc</title>
      <dc:creator>Nawaz Mujawar</dc:creator>
      <pubDate>Mon, 16 Oct 2023 17:50:16 +0000</pubDate>
      <link>https://forem.com/nawazmujawar/map-reduce-filter-sort-slice-etc-methods-of-array-in-js-2f9o</link>
      <guid>https://forem.com/nawazmujawar/map-reduce-filter-sort-slice-etc-methods-of-array-in-js-2f9o</guid>
      <description>&lt;p&gt;We are going to look into methods that are used to modify an Array in JavaScript.&lt;/p&gt;

&lt;p&gt;Here are the methods which are we going to look at in&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;push()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;pop()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;shift()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;unshift()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;filter()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;map()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;reduce()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;indexOf()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;includes()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;find()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;sort()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;splice()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;slice()&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  push()
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animals =["cat","dog"];
animals.push("tiger");
console.log(animals)// ["cat","dog","tiger"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;push()&lt;/code&gt; inserts the argument/value passed in &lt;code&gt;push("tiger")&lt;/code&gt; to the &lt;strong&gt;last&lt;/strong&gt; position in the &lt;code&gt;animals&lt;/code&gt; array.&lt;/p&gt;

&lt;h2&gt;
  
  
  pop()
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animals =["cat","dog","tiger","cow","elephant"];
animals.pop();
console.log(animals)// ["cat","dog","tiger","cow"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pop()&lt;/code&gt;removes the &lt;strong&gt;last&lt;/strong&gt; item of an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  shift()
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animals =["cat","dog"];
animals.shift("zebra");
console.log(animals)// ["zebra","cat","dog"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;shift()&lt;/code&gt; inserts the argument/value passed in &lt;code&gt;shift("zebra")&lt;/code&gt; to the &lt;strong&gt;first&lt;/strong&gt; position in the &lt;code&gt;animals&lt;/code&gt; array.&lt;/p&gt;

&lt;h2&gt;
  
  
  unshift()
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const animals =["cat","dog","tiger"];
animals.unshift();
console.log(animals)// ["dog","tiger"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;unshift()&lt;/code&gt; removes the &lt;strong&gt;first&lt;/strong&gt; item of an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  filter()
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbersArray =[12,34,312,67,98];

const newNumbersArray = filter((num)=&amp;gt;{
    if(num &amp;gt;50){
       return num;
    }
})
console.log(newNumbersArray) // [312,67,98]
console.log(numbersArray) //[12,34,312,67,98]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;filter()&lt;/code&gt; takes the first argument as a callback function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;filter()&lt;/code&gt; &lt;strong&gt;do not&lt;/strong&gt; change the original array, it returns a new copy of an array.&lt;/li&gt;
&lt;li&gt;As per the given condition it returns a new filtered array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  map()
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const users =[{name:"john",age:34},{name:"steve",age:40}];

const newUsersArray = filter((user)=&amp;gt;{
   return{
   detail :`${user.name} is ${user.age} years old`
   }
})
console.log(newUsersArray) 
// [
//   {detail:"john is 34 years old"},
//   {detail:"steve is 40 years old"}
// ]
console.log(users) 
//[{name:"john",age:34},{name:"steve",age:40}]

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map()&lt;/code&gt; loops over the each array item.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map()&lt;/code&gt; &lt;strong&gt;do not&lt;/strong&gt; change the original array, it returns a new copy of an array.&lt;/li&gt;
&lt;li&gt;In the above code snippet we have looped over &lt;code&gt;users&lt;/code&gt; array and returned a new array with concatenating  of name and age value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  reduce()
&lt;/h2&gt;

&lt;p&gt;Let's check with an example.&lt;/p&gt;

&lt;p&gt;Get the sum of all array items. i.e if an array is like &lt;code&gt;[1,2,3]&lt;/code&gt; then result should be &lt;code&gt;6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;first will try with &lt;code&gt;forEach()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sum =0;
const nums =[1,2,3];
nums.forEach((num)=&amp;gt;{
   sum = sum + num;
})
console.log(total) //6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now using &lt;code&gt;reduce()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums =[1,2,3];
const total = nums.reduce((acc ,num)=&amp;gt;{
   acc = acc + num;
  return acc
},0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the &lt;code&gt;reduce()&lt;/code&gt; it takes callback function as the first argument and the initial value of the accumulator &lt;code&gt;acc&lt;/code&gt;. And callback function provides two parameters first is accumulator &lt;code&gt;acc&lt;/code&gt; and second is array item &lt;code&gt;num&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;reduce()&lt;/code&gt; &lt;strong&gt;do not&lt;/strong&gt; change the original array.&lt;/p&gt;

&lt;p&gt;Returns single accumulated value.&lt;/p&gt;

&lt;h2&gt;
  
  
  indexOf()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;indexOf()&lt;/code&gt; returns the index of item.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const admins =[1,2,4];
const user ={name:"raj",id:4}
console.log(admins.indexOf(user.id)) //2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  includes()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;includes()&lt;/code&gt; returns a boolean depending upon whether the item is present or not in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const admins =[1,2,4];
const user ={name:"raj",id:4}
console.log(admins.indexOf(user.id)) // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  find()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;find()&lt;/code&gt; searches for the item in an array and returns that item.&lt;/p&gt;

&lt;p&gt;If the item is not present then return &lt;code&gt;-1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const users =[{name:"john",age:34},{name:"steve",age:40}]
const searchedUser = users.find((user)=&amp;gt;{
      if(user.name === "steve") return user;
})
console.log(searchedUser) // {name:"steve",age:40}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  sort()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;sort()&lt;/code&gt; sorts the given array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums =[2,8,4,3]
nums.sort()
console.log(nums) //[2,3,4,8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  splice()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;splice()&lt;/code&gt; changes the original array.&lt;/p&gt;

&lt;p&gt;Let's check an example for that. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deletion&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr =[2,3,5,1,6,8];
arr.splice(2,1)
console.log(arr)// [2,3,1,6,8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example &lt;code&gt;splice()&lt;/code&gt; first argument is the &lt;code&gt;index&lt;/code&gt; of items that needs to be deleted. And second argument is &lt;code&gt;number of times&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lets check another example for that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr =[2,3,5,1,6,8];
arr.splice(2,2)
console.log(arr)// [2,3,6,8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here it &lt;strong&gt;deletes&lt;/strong&gt; two items i.e 5 and 1 from index 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Insertion&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr =[2,3,5,1,6,8];
arr.splice(3,0,9,7)
console.log(arr)// [2,3,5,9,7,1,6,8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example &lt;code&gt;splice()&lt;/code&gt; first argument is &lt;code&gt;index&lt;/code&gt; where new items need to be inserted. the second argument is &lt;code&gt;0&lt;/code&gt; because we don't want to delete any item. The third argument is the items &lt;code&gt;9,7&lt;/code&gt; that need to be added to the array.&lt;/p&gt;

&lt;h2&gt;
  
  
  slice()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;slice()&lt;/code&gt; returns new array with selected items.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;slice()&lt;/code&gt; do not change original array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits =["apple","mango","banana","orange","watermelon"];

const favoriteFruits = fruits.slice(1,3);
console.log(favoriteFruits) // ["mango","banana"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;slice()&lt;/code&gt; first argument is starting index and second argument is ending index.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to match the backslash \  in Regular Expression?</title>
      <dc:creator>Nawaz Mujawar</dc:creator>
      <pubDate>Fri, 05 Feb 2021 12:55:43 +0000</pubDate>
      <link>https://forem.com/nawazmujawar/how-to-match-the-backslash-in-regular-expression-2kp4</link>
      <guid>https://forem.com/nawazmujawar/how-to-match-the-backslash-in-regular-expression-2kp4</guid>
      <description>&lt;p&gt;I want to match &lt;code&gt;&amp;lt;&amp;gt;/\":;=|*?&lt;/code&gt; these characters in the string, I tried this expression&lt;br&gt;
&lt;code&gt;&amp;lt;&amp;gt;/\":;=|*?&lt;/code&gt;&lt;br&gt;
But It didn't work for me.&lt;br&gt;
Please help me out. &lt;/p&gt;

</description>
      <category>help</category>
    </item>
    <item>
      <title>🥢Selector in Redux</title>
      <dc:creator>Nawaz Mujawar</dc:creator>
      <pubDate>Thu, 28 Jan 2021 08:46:19 +0000</pubDate>
      <link>https://forem.com/nawazmujawar/selector-in-redux-4eba</link>
      <guid>https://forem.com/nawazmujawar/selector-in-redux-4eba</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;A “selector” is simply a function that accepts Redux state as an argument and returns data that is derived from that state.&lt;/p&gt;

&lt;p&gt;A selector is a small function you write that can take the entire Redux state, and pick out a value from it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You know how &lt;code&gt;mapStateToProps&lt;/code&gt; works? How it takes the entire state and picks out values? Selectors basically do that. And, bonus, they improve performance too, by caching the values until state changes. Well – they can improve performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should you use a selector?
&lt;/h2&gt;

&lt;p&gt;It is a best practice to keep your Redux store state minimal and derive data from the state as needed. Selectors help with that. They can compute derived data, allowing Redux to store the minimal possible state. Selectors are also very efficient. A selector is not recomputed unless one of its arguments changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some examples of Selectors:
&lt;/h2&gt;

&lt;p&gt;Basic :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;selectUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Slightly Complex using ID:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;selectUserIds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More Complex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;selectUserIdsOfName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  An Example
&lt;/h2&gt;

&lt;p&gt;Redux gives you a store where you can put state. In a larger app, that state is usually an object, where each key of the object is managed by a separate reducer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;username&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;shoppingCart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;itemIds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;error&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;itemsById&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;error&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  First, without a selector
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When it comes time to get data out of the Redux state and into your React components, you’ll write a &lt;code&gt;mapStateToProps&lt;/code&gt; function that takes the entire state and cherry-picks the parts you need.&lt;/li&gt;
&lt;li&gt;Let’s say you want to show the items in the shopping cart. To do that, you need the items. Buuut the shoppingCart doesn’t have items. It only has item IDs. You have to take each ID and look it up in the products.items array. Here’s how you might do that:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;mapStateToProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shoppingCart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemIds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
      &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemsById&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Changing the state shape breaks &lt;code&gt;mapStateToProps&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now – what happens if you decide “You know… &lt;code&gt;shoppingCart&lt;/code&gt; should really be a property of the &lt;code&gt;currentUser&lt;/code&gt; instead of a standalone thing.” And then they reorganize the state to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;shoppingCart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;itemIds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;error&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;itemsById&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;error&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Well, now your previous &lt;code&gt;mapStateToProps&lt;/code&gt; function is broken. It refers to &lt;code&gt;state.shoppingCart&lt;/code&gt; which is now held at &lt;code&gt;state.currentUser.shoppingCart&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If you had a bunch of places in your app that referred to &lt;code&gt;state.shoppingCart&lt;/code&gt;, it’ll be a pain to update all of them. Fear or avoidance of that annoying update process might even prevent you from reorganizing the state when you know you should.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If only we had a way to centralize the knowledge of the shape of the state… some kind of &lt;em&gt;function&lt;/em&gt; we could call that knew how to find the data we wanted…&lt;/p&gt;

&lt;p&gt;Well, that’s exactly what a selector is for :)&lt;/p&gt;

&lt;h3&gt;
  
  
  Refactor: Write a simple selector
&lt;/h3&gt;

&lt;p&gt;Let’s rewrite the broken &lt;code&gt;mapStateToProps&lt;/code&gt; and pull out the state access into a selector.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// put this in some global-ish place,&lt;/span&gt;
&lt;span class="c1"&gt;// like selectors.js,&lt;/span&gt;
&lt;span class="c1"&gt;// and import it when you need to access this bit of state&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;selectShoppingCartItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shoppingCart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemIds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
    &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;itemsById&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;mapStateToProps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;selectShoppingCartItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next time the state shape changes, you can update that one selector and you’re done.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memoization
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The use of selectors in your application can also provide performance optimizations. Let’s say you have a component that needs to run an intensive sorting operation on the store’s state in order to get the data it needs. If you were to perform the operation in your mapStateToProps() function, without the use of a selector, the operation would run &lt;strong&gt;&lt;em&gt;every&lt;/em&gt;&lt;/strong&gt; time a dispatched action caused the state to update!&lt;/li&gt;
&lt;li&gt;It would be great if we could only run the expensive sorting operation only when the data we are running the operation on changes. This is where the concept of memoization comes to the rescue.&lt;/li&gt;
&lt;li&gt;Memoization is a form of caching. It involves tracking inputs to a function, and storing the inputs and the results for later reference. If a function is called with the same inputs as before, the function can skip doing the actual work, and return the same result it generated the last time it received those input values.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;A selector is a function that accepts Redux state as an argument and returns data that is derived from that state. Selectors can provide performance optimizations to your application and can also help you encapsulate your global state tree.&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>🚀Closure in Javascript👨‍💻 </title>
      <dc:creator>Nawaz Mujawar</dc:creator>
      <pubDate>Sun, 11 Oct 2020 10:22:47 +0000</pubDate>
      <link>https://forem.com/nawazmujawar/closure-in-javascript-54jo</link>
      <guid>https://forem.com/nawazmujawar/closure-in-javascript-54jo</guid>
      <description>&lt;p&gt;Hello World!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Closure?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;A &lt;em&gt;closure&lt;/em&gt; is the combination of a function and the lexical environment within which that function was declared.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Closure is an inner function that has access to outer(enclosing) function variables.&lt;/p&gt;

&lt;p&gt;Closure has 3 scope chains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;has access to Own scope&lt;/li&gt;
&lt;li&gt; has access to Outer function variables&lt;/li&gt;
&lt;li&gt; has access to Global variables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;we can access variables which are outside of function&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using closure
&lt;/h2&gt;

&lt;p&gt;Closure are used for data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s &lt;strong&gt;privileged methods&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;let  see example of  closure :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; 
     &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;displayAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; year old.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="nx"&gt;displayAge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;22&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Output : John is 22 year old.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;In above example , &lt;code&gt;User()&lt;/code&gt; is the outer function that returns  inner function &lt;code&gt;displayAge()&lt;/code&gt;. Inner function has access to outer scope variable even though outer function is returned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Closure are nothing but the inner or nested function that has access to variable that are in outer scope.&lt;/p&gt;

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