← 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.

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"}
51

Convert to JSON replacing values with toJSON method

2021-04-06

1// JS Tip of the Day
2// Convert to JSON replacing values with toJSON method
3
4const hero = {
5    name: 'Batman',
6    isRich: true,
7    weapons: { primary: 'batarang', secondary: 'Shark Repellent' },
8    vehicles: ['batpod', 'tumbler'],
9    partner: null
10};
11
12hero.weapons.toJSON = function() {
13    return this.primary;
14}
15console.log(JSON.stringify(hero));
16// {"name":"Batman","isRich":true,"weapons":"batarang","vehicles":["batpod","tumbler"],"partner":null}

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.