<?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: jav7zaid</title>
    <description>The latest articles on DEV Community by jav7zaid (@jav7zaid).</description>
    <link>https://dev.to/jav7zaid</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%2F506341%2F4421eeba-217f-4413-bc54-ea7782912641.png</url>
      <title>DEV Community: jav7zaid</title>
      <link>https://dev.to/jav7zaid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jav7zaid"/>
    <language>en</language>
    <item>
      <title>How to conditionally select array or array of objects in Javascript ?</title>
      <dc:creator>jav7zaid</dc:creator>
      <pubDate>Mon, 21 Jun 2021 18:18:18 +0000</pubDate>
      <link>https://dev.to/jav7zaid/how-to-conditionally-select-array-or-array-of-objects-in-javascript-4i7k</link>
      <guid>https://dev.to/jav7zaid/how-to-conditionally-select-array-or-array-of-objects-in-javascript-4i7k</guid>
      <description>&lt;p&gt;If you have ever come across a use case where you have multiple arrays like below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1,2,3];
const array2 = [4,5,6];
const array3 = [{ a: 1 }, { b: 2 }];

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

&lt;/div&gt;



&lt;p&gt;What if you want the resultant array to conditionally include one or more arrays, one way to do it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrayWeWant = [];

if(condition1) {
 arrayWeWant.push(array1);
}

if(condition2) {
 arrayWeWant.push(array2);
}

if(condition3) {
 arrayWeWant.push(array3);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;well that's not a bad approach, but we can do better&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrayWeWant = [
  ...(condtion1 ? array1 : []),
  ...(conditon2 ? array2 : []),
  ...(conditon3 ? array3 : [])
];

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

&lt;/div&gt;



&lt;p&gt;It's a much cleaner way isn't it.&lt;/p&gt;

&lt;p&gt;Happy to share!😊&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
