← Torna al blog

Dev Tips

Un trucchetto al giorno leva le complicazioni di torno! Piccole "scorciatoie" che possono facilitare la vita degli sviluppatori e far risparmiare tempo...e righe di codice.

56

How to make an object immutable

2021-04-12

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
55

Cardinality of Items

2021-04-09

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 }
54

Remove duplicates from array

2021-04-09

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
53

Detecting an Array

2021-04-08

1// JS Tip of the Day
2// Detecting an Array
3
4console.log(Array.isArray([]));  // true
5console.log(Array.isArray(['Batman', 'Superman']));  // true
6console.log(Array.isArray({ 0: 'Batman', 1: 'Superman', length: 2})); // false with array-like objects
52

Quick Conditional Object Properties

2021-04-07

1// JS Tip of the Day
2// Quick Conditional Object Properties
3
4const haveWeapon = true;
5const havePartner = false;
6
7const hero = {
8  name: 'Batman',
9  superPower: 'money',
10  ...(haveWeapon ? { weapon: 'Batarang' } : {}),
11  ...(havePartner ? { partner: 'Robin' } : {})
12};
13
14console.log(hero); // {name: "Batman", superPower: "money", weapon: "Batarang"}

Contattaci.

Hai in mente un progetto e vorresti realizzarlo?
Sei interessato a migliorare le competenze del tuo team in ambito di programmazione e sviluppo?
Oppure vuoi semplicemente prendere prendere un caffè con noi e vedere la nostra collezione di Action Figure, allora scrivici tramite questo form.

Se, invece, vuoi far parte del team, guarda le nostre offerte di lavoro.

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