<?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: CodErdal</title>
    <description>The latest articles on Forem by CodErdal (@coderdal).</description>
    <link>https://forem.com/coderdal</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%2F824944%2F3ab25d55-67df-40fc-a5f9-14a3e31e2f44.png</url>
      <title>Forem: CodErdal</title>
      <link>https://forem.com/coderdal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/coderdal"/>
    <language>en</language>
    <item>
      <title>How to Conditionally Add Property to the Object in JavaScript</title>
      <dc:creator>CodErdal</dc:creator>
      <pubDate>Wed, 12 Jul 2023 15:35:39 +0000</pubDate>
      <link>https://forem.com/coderdal/how-to-conditionally-add-property-to-the-object-in-javascript-2916</link>
      <guid>https://forem.com/coderdal/how-to-conditionally-add-property-to-the-object-in-javascript-2916</guid>
      <description>&lt;p&gt;In &lt;strong&gt;JavaScript&lt;/strong&gt;, there are several ways to conditionally add elements to an object based on certain conditions. This flexibility allows us to dynamically modify objects as needed. In this post, i will talk about two different ways to accomplish this task. Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 1:&lt;/strong&gt; Spread Operator and Logical AND Operator&lt;br&gt;
This method allows you to add items to the object depending on the condition. If the condition is not true, no new item is added to the object.&lt;/p&gt;

&lt;p&gt;Example:&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;let&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="p"&gt;...(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="nx"&gt;here&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;propvalue&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;otherprops&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Method 2:&lt;/strong&gt; Ternary Operator&lt;br&gt;
This method only allows you to add the value of the key conditionally, the element is defined to the object in both conditions.&lt;/p&gt;

&lt;p&gt;Example:&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;let&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;newKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;defaultValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Can be useful:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>data</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How To Pass Arguments To The Vuex Getters</title>
      <dc:creator>CodErdal</dc:creator>
      <pubDate>Tue, 27 Sep 2022 16:30:09 +0000</pubDate>
      <link>https://forem.com/coderdal/how-to-pass-arguments-to-the-vuex-getters-1dkk</link>
      <guid>https://forem.com/coderdal/how-to-pass-arguments-to-the-vuex-getters-1dkk</guid>
      <description>&lt;p&gt;We use &lt;strong&gt;Vuex&lt;/strong&gt; to do more systematic state management with &lt;strong&gt;Vue JS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getters&lt;/strong&gt; with a &lt;strong&gt;Vuex&lt;/strong&gt; property are written like functions but work like computed properties.&lt;/p&gt;

&lt;p&gt;Sometimes; When using &lt;strong&gt;getters&lt;/strong&gt; in &lt;strong&gt;Vuex&lt;/strong&gt;, in some cases we may want to send some arguments to our &lt;strong&gt;getters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To do this we can use the "Method-Style Access" method provided by vuex.&lt;/p&gt;

&lt;p&gt;Using this method we should define our getters like this:&lt;/p&gt;

&lt;p&gt;Defining:&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="nx"&gt;getterName&lt;/span&gt;&lt;span class="p"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// get the parameter&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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&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;===&lt;/span&gt; &lt;span class="nx"&gt;parameter&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;Using this defining method, we can send arguments to the getters we define.&lt;/p&gt;

&lt;p&gt;Usage:&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="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getterName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// we sent 2 as getter parameter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But sometimes we can include our getters with the "mapGetters" helper. &lt;br&gt;
In this case, we must use a different method to send arguments to the getters.&lt;/p&gt;
&lt;h2&gt;
  
  
  Sending arguments to getter included with mapGetters
&lt;/h2&gt;

&lt;p&gt;After defining the getter with the same method, we include it in the component with "mapGetters".&lt;/p&gt;

&lt;p&gt;To send an argument to a getter included with mapGetters; we need to define a new computed property.&lt;/p&gt;

&lt;p&gt;This new computed property will take parameter like a function. It will send this parameter as an argument to the getter that comes with mapGetters and return this value.&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;mapGetters&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vuex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nl"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// import the getters with mapGetters&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;mapGetters&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getterName&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="c1"&gt;//Define New Computed Property&lt;/span&gt;
    &lt;span class="nx"&gt;newGetter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="c1"&gt;// get the parameter&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getterName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// return the value&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;Thus, the arguments we send to our getter named &lt;strong&gt;newGetter&lt;/strong&gt; will be sent to the getter we include with mapGetters.&lt;/p&gt;

&lt;p&gt;Method-Style Access:&lt;br&gt;
&lt;a href="https://vuex.vuejs.org/guide/getters.html#method-style-access"&gt;https://vuex.vuejs.org/guide/getters.html#method-style-access&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>vuex</category>
    </item>
  </channel>
</rss>
