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

11

String occurrences quick replacement

2021-02-08

1// JS Tip of the Day
2// String occurrences quick replacement
3
4const heroes = '🦇 🤜 🕷️ 🤜 🐜';
5
6// with RegEx
7const heroesHandshake = heroes.replace(/\🤜/g, '🤝');
8console.log(heroesHandshake); // 🦇 🤝 🕷️ 🤝  🐜
9
10// without RegEx - ES2021/ES12
11const heroesHandshakeReplaceAll = heroes.replaceAll('🤜', '🤝');
12console.log(heroesHandshakeReplaceAll); // 🦇 🤝 🕷️ 🤝  🐜
10

Prettify JSON Stringify

2021-02-05

1// JS Tip of the Day
2// Prettify JSON Stringify
3
4const hero = { name: "Batman", weapon: "Batarang", vehicle: "Batmobile" };
5
6// Prettify with a String 
7console.log(JSON.stringify(hero, null, '--> '));
8/*
9{
10--> "name": "Batman",
11--> "weapon": "Batarang",
12--> "vehicle": "Batmobile"
13}
14*/
15
16// Prettify with a Tabulation Char 
17console.log(JSON.stringify(hero, null, '\t'));
18/*
19{
20        "name": "Batman",
21        "weapon": "Batarang",
22        "vehicle": "Batmobile"
23}
24*/
25
26// Prettify with a Number of Spaces
27console.log(JSON.stringify(hero, null, 2));
28/*
29{
30  "name": "Batman",
31  "weapon": "Batarang",
32  "vehicle": "Batmobile"
33}
34*/
9

Create pure Object

2021-02-04

1// JS Tip of the Day
2// Create pure Object
3
4// Object with constructor, prototype, ...
5const hero = { name: "Batman", weapon: "Batarang", vehicle: "Batmobile" };
6console.log(hero); // { name: 'Batman', weapon: 'Batarang', vehicle: 'Batmobile' }
7console.log(hero.constructor); // [Function: Object]
8console.log(hero.toString); // [Function: toString]
9console.log(hero.hasOwnProperty); // [Function: hasOwnProperty]
10
11// Object without constructor, prototype, ...
12const pureHero = Object.create(null);
13Object.assign(pureHero, { name: "Batman", weapon: "Batarang", vehicle: "Batmobile" });
14
15console.log(pureHero); // { name: 'Batman', weapon: 'Batarang', vehicle: 'Batmobile' }
16console.log(pureHero.__proto__); // undefined
17console.log(pureHero.constructor); // undefined
18console.log(pureHero.toString); // undefined
19console.log(pureHero.hasOwnProperty); // undefined
8

Rounding Numbers

2021-02-03

1// JS Tip of the Day
2// Rounding Numbers
3
4const myNumber = 3.14159;
5
6// Rounding Nearest
7console.log('Math.round', Math.round(myNumber)); // 3
8
9// Rounding Up
10console.log('Math.ceil', Math.ceil(myNumber)); // 4
11
12// Rounding Down
13console.log('Math.floor', +Math.floor(myNumber)); // 3
14
15// Rounding to a specified number of decimals - 2nd digit example
16// multiply and divide by 100 or a bigger power of 10 for more digits
17console.log('Math.ceil', Math.ceil(myNumber)); // 3.14
18
19// Rounding to a specified number of decimals - 2nd digit example
20// toFixed return a string and we have to convert it to a number
21console.log('Number.toFixed', +myNumber.toFixed(2)); // 3.14
22
23// Removing anything after the decimal point - ES6/ES2015
24console.log('Math.trunc', Math.trunc(myNumber)); // 3
7

Object to Array and vice versa

2021-02-02

1// JS Tip of the Day
2// Object to Array and vice versa
3
4const hero = {
5  name: 'Batman',
6  weapon: 'Batarang',
7  vehicle: 'Batmobile'
8};
9
10// From Object to Array
11const heroEntries = Object.entries(hero); // ES2017/ES8
12console.log(heroEntries);
13// [["name", "Batman"], ["weapon", "Batarang"], ["vehicle", "Batmobile"]]
14
15// From Array to Object
16const anotherHero = Object.fromEntries(heroEntries); // ES2019/ES10
17console.log(anotherHero);
18// { name: 'Batman',  weapon: 'Batarang',  vehicle: 'Batmobile' }
19
20
21

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.