The complete Javascript Latest Learning Resources and Articles to guide you

Header Ads

Thursday, 12 September 2019

12 Most Essential Javascript Tricks Every developer should know

0 comments

remove duplicates from an Array

new SET() method

"The Set object lets you store unique values of any type, whether primitive values or object reference"

.from method

The Array.from() method returns an array object from any object with a lengthy property or an iterable object.

Replace the specific value in an array

to remove specific value in the array you need to use .splice method(start,value to remove,valueToAdd). You need to provide first start value then the value we would like to remove and then the value what we want.

Map array without .map()

Every developers know to use .map() method but the few of them know we can also use .from() method.
var friendship = [{ name: ‘John’, age: 20 },{ name: ‘Peter’, age: 23 },{ name: ‘Mark’, age: 24 },{ name: ‘Maria’, age: 22 },{ name: ‘Monica’, age: 21 },{ name: ‘Martha’, age: 19 },]var friendsNames = Array.from(friendship, ({name}) => name);console.log(friendsNames); // returns [“John”, “Peter”, “Mark”, “Maria”, “Monica”, “Martha”]

Empty an array

Do you have a array loaded with components(elements) however you have to clean it for any reason, and you would prefer not to expel things individually? It's easy to do it in one line of code. To remove an array you just need to set array's length to zero.

convert array to an object

ECMAScript 6 introduces the easily polyfillable Object.assign:
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
Object.assign({}, ['a','b','c']); // {0:"a", 1:"b", 2:"c"}

fulfill array with data

There are a few circumstances when we make an exhibit, and we might want to fill it with certain information, or we need an array with similar qualities, and for this situation .fill() technique accompanies a simple and clean arrangement.

merge arrays

the Concat() method is used to join two or more arrays.
This method does not change the existing array,but returns a new array by joining the value of arrays.

find the intersection of two arrays

Use a combination of Array.prototype.filter and Array.prototype.indexOf:

array1.filter(value => -1 !== array2.indexOf(value))
Or as vrugtehagel suggested in the comments, you can use the more recent Array.prototype.includes for even simpler code:

array1.filter(value => array2.includes(value))
For older browsers:

array1.filter(function(n) {
    return array2.indexOf(n) !== -1;
});

remove falsy values from an array

Simply use a filter() method to do this.

get random value from an array

Use this simple line of code and implement it in your code.
var rand = myArray[Math.floor(Math.random() * myArray.length)];

Caching of Variable

Use localStorage for that. It's persistent over sessions.
Writing :
localStorage['myKey'] = 'somestring'; // only strings
Reading :
var myVar = localStorage['myKey'] || 'defaultValue';
If you need to store complex structures, you might serialize them in JSON. For example :
Reading :
var stored = localStorage['myKey'];
if (stored) myVar = JSON.parse(stored);
else myVar = {a:'test', b: [1, 2, 3]};
Writing :
localStorage['myKey'] = JSON.stringify(myVar);
Note that you may use more than one key. They'll all be retrieved by all pages on the same domain.
Unless you want to be compatible with IE7, you have no reason to use the obsolete and small cookies.

TYPECASTING IN JAVASCRIPT

Converting a data type into another is known as type casting.

typeof

it is used to return the data type of an operand
typeof operand;

Converting to Boolean

Boolean(value);

Convert a value to string data type

String(value);

ParseInt() Method

parseint() function is used to convert string into numbers.It doesn't work for boolean or data-types values.

ParseFloat()

parseFloat() function is used to convert strings values into floating point. It works similar like parseInt() function with an exception of handling decimal point numbers.

No comments:

Post a Comment