Tip of the day - JavaScript

Un trucchetto al giorno per mantenerti allenato o per conoscere qualcosa di nuovo sul magico mondo di JavaScript.

58

Simple and Quick Array Comparison

2021-04-14 - JavaScript

1// JS Tip of the Day
2// Simple and Quick Array Comparison
3
4const arrayComparison = (firstArray, secondArray) => {
5  return JSON.stringify(firstArray) === JSON.stringify(secondArray);
6}
7
8const heroes = ['Batman', 'Superman', 'Green Arrow'];
9const moreHeroes = ['Batman', 'Superman', 'Green Arrow'];
10
11arrayComparison(heroes, moreHeroes); // true
12/*
13	Attention!
14	It falls with different values serialized but 
15    with the same string conversion like null, undefined, ...
16*/
#JStipoftheday
57

Set an Object's non-enumerable property

2021-04-13 - JavaScript

1// JS Tip of the Day
2// Set an Object's non-enumerable property
3
4const hero = { name: 'Batman', weapon: 'Batarang', partner: 'Robin' };
5
6for (let [key, value] of Object.entries(hero)) {
7    console.log(key, value); 
8    // ['name', 'Batman'], ['weapon', 'Batarang'], ['partner', 'Robin']
9}
10
11// Set enumerable data descriptor of an Object's property to false
12Object.defineProperty(hero, 'partner', { enumerable: false });
13
14for (let [key, value] of Object.entries(hero)) {
15    console.log(key, value);
16    // ['name', 'Batman'], ['weapon', 'Batarang']
17}
18
19// Get only enumerable properties with common methods or loops
20console.log(Object.keys(hero)); 
21// Get all properties (without Symbol properties)
22console.log(Object.getOwnPropertyNames(hero)); 
23
#JStipoftheday
56

How to make an object immutable

2021-04-12 - JavaScript

1// JS Tip of the Day
2// How to make an object immutable
3
4const hero = {
5  name: 'Batman',
6  weapon: 'Batarang',
7  vehicle: {
8    type: 'car',
9    model: 'Batmobile'
10  }
11};
12
13hero.name = 'Superman';
14hero.partner = 'Robin';
15
16// Check if object is frozen
17console.log(Object.isFrozen(hero)); // false
18
19// Freeze the object
20Object.freeze(hero); // Oh yeah, Mr. Freeze!!!!
21
22hero.name = 'Green Arrow'; // silent failure or TypeError in strict mode
23delete hero.partner; // silent failure or TypeError in strict mode
24
25// Check if object is frozen
26console.log(Object.isFrozen(hero)); // true
27
28hero.vehicle.model = 'Batpod'; // It is possibile! Object.freeze isn't a deep method!
29console.log(hero); 
30/*
31{
32  name: 'Superman',
33  weapon: 'Batarang',
34  vehicle: {
35    type: 'car',
36    model: 'Batpod'
37  },
38  partner: 'Robin'
39}
40
#JStipoftheday
55

Cardinality of Items

2021-04-09 - JavaScript

1// JS Tip of the Day
2// Cardinality of Items
3
4const heroes = [
5    { id: 1, name: "Batman", year: 1939, superPower: "Money" },
6    { id: 2, name: "Superman", year: 1938, superPower: "Super Strength" },
7    { id: 3, name: "Acquaman", year: 1940, superPower: "Telepathy" },
8    { id: 3, name: "Wonder Woman", year: 1940, superPower: "Super Strength" },
9];
10
11const cardinality = (arr, field) => {
12    return arr.reduce((acc, item) => {
13        acc[item[field]] = acc[item[field]] + 1 || 1;
14        return acc;
15    }, {});
16}
17
18console.log(cardinality(heroes, 'year')); 
19// { '1938': 1, '1939': 1, '1940': 2 }
20console.log(cardinality(heroes, 'superPower')); 
21// { Money: 1, 'Super Strength': 2, Telepathy: 1 }
#JStipoftheday
54

Remove duplicates from array

2021-04-09 - JavaScript

1// JS Tip of the Day
2// Remove duplicates from array
3
4const heroes = ['Batman', 'Superman', 'Superman', 'Acquaman', 'Batman', 'Wonder Woman'];
5const uniqueValuesHeroes = [...new Set(heroes)];
6console.log(uniqueValuesHeroes);
7// output: ['Batman', 'Superman', 'Acquaman', 'Wonder Woman']
8
#JStipoftheday

Preferisci ricevere i tips via mail? Iscriviti alla newsletter.

Iscriviti alla newsletter

Tip of the day - JavaScript

Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le tue competenze o quelle del tuo team in ambito di programmazione e sviluppo?

Anche se - semplicemente - vuoi prendere un caffè con noi o vedere la nostra collezione di Action Figures scrivici tramite questo form.

Questo sito è protetto da reCAPTCHA e si applicano le Norme sulla privacy e i Termini di servizio di Google.