<?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: Sedat Can Yalçın</title>
    <description>The latest articles on DEV Community by Sedat Can Yalçın (@sedat).</description>
    <link>https://dev.to/sedat</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%2F202829%2F0d1e9acd-2c36-4c91-b567-1b70dc229999.jpeg</url>
      <title>DEV Community: Sedat Can Yalçın</title>
      <link>https://dev.to/sedat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sedat"/>
    <language>en</language>
    <item>
      <title>JS Bits - Destructuring arrays in JavaScript</title>
      <dc:creator>Sedat Can Yalçın</dc:creator>
      <pubDate>Thu, 13 Aug 2020 00:01:02 +0000</pubDate>
      <link>https://dev.to/sedat/destructuring-arrays-in-javascript-rocket-126d</link>
      <guid>https://dev.to/sedat/destructuring-arrays-in-javascript-rocket-126d</guid>
      <description>&lt;h1&gt;
  
  
  Destructuring
&lt;/h1&gt;

&lt;p&gt;Destructuring is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. (&lt;a href="https://dev.to/sarah_chima/destructuring-assignment---arrays-16f"&gt;https://dev.to/sarah_chima/destructuring-assignment---arrays-16f&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Rather than getting a whole data, with destructuring we can only retrieve the values we want.&lt;/p&gt;

&lt;h2&gt;
  
  
  Destructuring arrays
&lt;/h2&gt;

&lt;p&gt;We have a function groceries which returns us our list of items that we wish to buy next time we go to a supermarket.&lt;/p&gt;

&lt;p&gt;The data that groceries function returns is as follows; [bread, apples, cheese]&lt;/p&gt;

&lt;p&gt;In a traditional sense we would get a reference to each item in this way;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const groceriesList = groceries();
const bread = groceriesList[0];
const apples = groceriesList[1];
const cheese = groceriesList[2];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Destructuring allows us to achieve this in an elegant and simple way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [
    bread,
    apples,
    cheese
] = groceries()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you also want a reference to the groceries list all you need to do is;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [
    bread,
    apples,
    cheese
] = groceriesList = groceries()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But, what happens if groceries returns us an array with four values?&lt;/p&gt;

&lt;p&gt;Simply, we would only get the first three values without touching the fourth value.&lt;/p&gt;

&lt;p&gt;What happens when we want to retrieve three values but groceries function returns two values?&lt;/p&gt;

&lt;p&gt;Let's say the array doesn't have &lt;strong&gt;cheese value&lt;/strong&gt; and we want to reference to this value with the &lt;strong&gt;cheese variable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The code would not break and the cheese variable will simply be &lt;strong&gt;undefined&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Undefined values
&lt;/h3&gt;

&lt;p&gt;Another case is when a value is undefined.&lt;/p&gt;

&lt;p&gt;Imperatively if a value can be undefined and we want a default value to it when it's undefined.&lt;/p&gt;

&lt;p&gt;We would do;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = names[0] !== undefined ? names[0] : 'unregistered'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Declaratively, using destructuring we do;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [
name = 'unregistered'
] = names
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  What if we want to get the first three into their own variables and the rest into one variable?
&lt;/h3&gt;

&lt;p&gt;In order to achieve this we use the spread operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [
    bread,
    apples,
    cheese
    // get the first three as normal then
    ...others // gather the rest of them and put them into variable others
] = groceries()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Variable swapping with destructuring
&lt;/h3&gt;

&lt;p&gt;Destructuring allows a handy trick for swapping two variables without the need for a temp variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[x,y] = [y,x]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Destructuring function paramaters
&lt;/h3&gt;

&lt;p&gt;You can destructure an array which is a parameter to a function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1,2,3,4,5]
const firstThree = ([one = 10, two, three]) =&amp;gt; {
    // do something
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The parameter 10 has a default value of 10 if it receives undefined&lt;/p&gt;

&lt;h3&gt;
  
  
  Destructuring nested arrays
&lt;/h3&gt;

&lt;p&gt;We can destructure nested array using another pair of brackets inside our brackets&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const data = [1,[2,3],4]
const [
    one,
    [two, three] = [], // fallback to an empty array if undefined
    four

]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Thank you for reading! &lt;br&gt;
If you have any questions please let me know!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JS Bits - Array Methods</title>
      <dc:creator>Sedat Can Yalçın</dc:creator>
      <pubDate>Sun, 19 Jul 2020 18:28:36 +0000</pubDate>
      <link>https://dev.to/sedat/js-bits-array-methods-3dd7</link>
      <guid>https://dev.to/sedat/js-bits-array-methods-3dd7</guid>
      <description>&lt;h1&gt;
  
  
  Welcome to JS Bits
&lt;/h1&gt;

&lt;p&gt;Hello everyone, welcome to the first post on my new series JS Bits where I explain and also show you use cases where you can use them. &lt;/p&gt;

&lt;p&gt;This post was originally posted on my &lt;a href="https://sedat.dev/blog/"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Find it &lt;a href="https://sedat.dev/blog/js-bits-array-methods/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Array Methods
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Array.prototype.find(callback)
&lt;/h3&gt;

&lt;p&gt;Takes a callback function that returns the value of the first element that satisfies the condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// found is equal to 130. It found the value 130 in the third index and returned it.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Array.prototype.findIndex(callback)
&lt;/h3&gt;

&lt;p&gt;Similar to find method, returns the index of the first value that satisfies the condition&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isLargeNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findIndex&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&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;element&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// return the index of value 4 which is 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Array.prototype.includes(valueToFind[, fromIndex])
&lt;/h3&gt;

&lt;p&gt;Returns &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; whether the array includes the given value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As an optional argument includes takes a parameter fromIndex which means where to begin the searching for the valueToFind&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&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;// starts searching the value 5 in arr beginning from the second index which returns true.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Array.prototype.flat([depth])
&lt;/h3&gt;

&lt;p&gt;Creates a new flattend array with all the sub-array (nested parts) values taken out and concentinated in to the higher depth.&lt;/p&gt;

&lt;p&gt;You will understand it better with this example;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// we have a nested array like this&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; 
&lt;span class="c1"&gt;// this array has [3,4] which goes one level deeper and empty array of [[]] which goes two levels deeper&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// default value is one, returns the array: [1,2,3,4,5,6,[7]]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;otherARr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&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;// goes to the depth of two and returns: [1,2,3,4,5,6,7]&lt;/span&gt;

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



&lt;h3&gt;
  
  
  Array.prototype.flatMap(callback(currentValue[, index[, array]])
&lt;/h3&gt;

&lt;p&gt;It is very common now to use functional programming or FP methods in JavaScript like map().&lt;/p&gt;

&lt;p&gt;If you want to use &lt;code&gt;flat()&lt;/code&gt; on an array you are mapping. You have to first &lt;code&gt;map()&lt;/code&gt;, which creates a new array and then call the &lt;code&gt;flat()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;flatMap()&lt;/code&gt; combines a map and a flat with a &lt;strong&gt;depth of one&lt;/strong&gt; by first mapping each element and then running flat on the newly created array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubleArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flatMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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;x&lt;/span&gt; &lt;span class="o"&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;// [2, 4, 6, 8]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A very good use case of &lt;code&gt;flatMap()&lt;/code&gt; is adding or removing items during a &lt;code&gt;map()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;returnDoubles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;flatMap&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;v&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&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="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;// [2,4,6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Keep in mind that if you want to run &lt;code&gt;flatMap()&lt;/code&gt; with a depth different than one(1). You have to call &lt;code&gt;flat()&lt;/code&gt;&lt;br&gt;
additionally with a &lt;code&gt;map()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Want to learn more about me? Here's my &lt;a href="https://sedat.dev"&gt;portfolio&lt;/a&gt;&lt;/p&gt;

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