<?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: Artem Voevoda</title>
    <description>The latest articles on DEV Community by Artem Voevoda (@temcodes).</description>
    <link>https://dev.to/temcodes</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%2F112884%2Fe94ecdce-9eef-4804-a827-05e9148f03b9.png</url>
      <title>DEV Community: Artem Voevoda</title>
      <link>https://dev.to/temcodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/temcodes"/>
    <language>en</language>
    <item>
      <title>7 useful links for javascript</title>
      <dc:creator>Artem Voevoda</dc:creator>
      <pubDate>Thu, 20 Jun 2019 09:50:19 +0000</pubDate>
      <link>https://dev.to/temcodes/7-useful-links-for-javascript-33j1</link>
      <guid>https://dev.to/temcodes/7-useful-links-for-javascript-33j1</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. Reduction of values to a logical type&lt;/strong&gt;&lt;br&gt;
Here's how to bring a certain value to a logical type:&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;const&lt;/span&gt; &lt;span class="nx"&gt;myBoolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;myVariable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Double negation (!!) is necessary in order for a value that is true from the point of view of JavaScript rules to be converted to true and false to false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Remove duplicate values ​​in arrays&lt;/strong&gt;&lt;br&gt;
Here's how to remove duplicate values ​​from an array:&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;const&lt;/span&gt; &lt;span class="nx"&gt;deDupe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The specified data structures store only unique values. As a result, the use of such a data structure and syntactic scattering allows you to create a new array based on the array myArray, in which there are no duplicate values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Creating and setting object properties by condition&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To set properties of objects using the &amp;amp;&amp;amp; operator, you can use the distribution syntax:&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;const&lt;/span&gt; &lt;span class="nx"&gt;myObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt; &lt;span class="nx"&gt;myProperty&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;propName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;myProperty&lt;/span&gt;&lt;span class="p"&gt;}};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If as a result of the calculation of the left side of the expression, something is received that is perceived by JS as a false value, then &amp;amp;&amp;amp; will not perform further calculations, and the new property will not be created and set. MyObject will be empty. If the ... myProperty construction returns some result that JS perceived as true, thanks to the &amp;amp;&amp;amp; construction, the propName property will appear in the object, preserving the resulting value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Merge objects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's how to create a new object in which two other objects will be merged:&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;const&lt;/span&gt; &lt;span class="nx"&gt;mergedObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt; &lt;span class="nx"&gt;objectOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;objectTwo&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach can be used to organize the merger of an unlimited number of objects. Moreover, if objects have properties with the same name, in the final object there will be only one such property belonging to the source objects, which is located to the right of the others. Please note that this is done using shallow copying of object properties.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Exchange of variable values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To exchange values ​​between two variables without using an auxiliary variable, you can do this:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;varA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;varB&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;varB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;varA&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, what was in varA will fall into varB, and vice versa. This is possible through the use of internal mechanisms of destruction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Removing False Values ​​from an Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's how to remove from the array all values ​​that are considered false in JavaScript:&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;const&lt;/span&gt; &lt;span class="nx"&gt;clean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dirty&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="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During the execution of this operation, values ​​such as null, undefined, false, 0, as well as empty lines will be removed from the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Converting numbers to strings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To convert numbers stored in an array to their string representation, you can do this:&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;const&lt;/span&gt; &lt;span class="nx"&gt;stringArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numberArray&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="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The string elements of the array during such a conversion will remain string.&lt;/p&gt;

&lt;p&gt;You can also perform the inverse transform by converting String values ​​to Number values:&lt;/p&gt;

&lt;p&gt;const numberArray = stringArray.map (Number);&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion: on merging and expanding a single-line code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What examples of useful JS one-line users would you add to this material?&lt;/p&gt;

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