DEV Community

Artem Voevoda
Artem Voevoda

Posted on • Updated on

7 useful links for javascript

1. Reduction of values to a logical type
Here's how to bring a certain value to a logical type:

const myBoolean = !!myVariable;
Enter fullscreen mode Exit fullscreen mode

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.

2. Remove duplicate values ​​in arrays
Here's how to remove duplicate values ​​from an array:

const deDupe = [... new Set (myArray)];
Enter fullscreen mode Exit fullscreen mode

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.

3. Creating and setting object properties by condition

To set properties of objects using the && operator, you can use the distribution syntax:

const myObject = {... myProperty && {propName: myProperty}};
Enter fullscreen mode Exit fullscreen mode

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 && 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 && construction, the propName property will appear in the object, preserving the resulting value.

4. Merge objects

Here's how to create a new object in which two other objects will be merged:

const mergedObject = {... objectOne, ... objectTwo};
Enter fullscreen mode Exit fullscreen mode

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.

5. Exchange of variable values

To exchange values ​​between two variables without using an auxiliary variable, you can do this:

[varA, varB] = [varB, varA];
Enter fullscreen mode Exit fullscreen mode

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

6. Removing False Values ​​from an Array

Here's how to remove from the array all values ​​that are considered false in JavaScript:

const clean = dirty.filter (Boolean);
Enter fullscreen mode Exit fullscreen mode

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

7. Converting numbers to strings

To convert numbers stored in an array to their string representation, you can do this:

const stringArray = numberArray.map (String);
Enter fullscreen mode Exit fullscreen mode

The string elements of the array during such a conversion will remain string.

You can also perform the inverse transform by converting String values ​​to Number values:

const numberArray = stringArray.map (Number);

Conclusion: on merging and expanding a single-line code

What examples of useful JS one-line users would you add to this material?

Top comments (1)

Collapse
 
keshavdutt profile image
keshavdutt

Here is another link for showing how to convert strings into array in javascript