← 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.
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.
2021-02-01
1// JS Tip of the Day
2// Simple If with multiple conditions
3
4// Long version
5if (hero === 'Batman' || hero === 'Superman' || hero === 'GreenArrow') {
6 // ...
7}
8
9// Short version
10if (['Batman', 'Superman', 'GreenArrow'].includes(hero)) {
11 // ...
12}
13
2021-01-29
1// JS Tip of the Day
2// How to get a (real) string length
3
4const bestHero = "Batman";
5console.log(bestHero.length); // 6
6
7const word = "𥑮"; // Surrogate pairs
8console.log(word.length); // 2
9console.log([...word].length); // 1
10
11const rocket = "🚀"; // Surrogate pairs
12console.log(rocket.length); // 2
13console.log([...rocket].length); // 1
14
15const bestDirector = "Rene\u0301"; // Combining marks
16console.log(bestDirector); // René
17console.log(bestDirector.length); // 5
18console.log(bestDirector.normalize()); // René
19console.log(bestDirector.normalize().length); // 4
2021-01-28
1// JS Tip of the Day
2// Flat an array with the nested arrays - ES10/ES2019
3
4const arr = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
5
6// Depth Level is 1
7console.log(arr.flat()); // [1, 2, 3, 4, [5, 6, [7, 8, [9, 10]]]];
8// or
9console.log(arr.flat(1)); // [1, 2, 3, 4, [5, 6, [7, 8, [9, 10]]]];
10
11// Depth Level is 2
12console.log(arr.flat(2)); // [1, 2, 3, 4, 5, 6, [7, 8, [9, 10]]];
13
14// Infinitely Deep
15console.log(arr.flat(Infinity)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2021-01-27
1// JS Tip of the Day
2// Merge two (or more) Arrays and remove duplicate items.
3
4const firstArray = [2, 4, 6, 5], secondArray = [6, 11, 5, 3];
5const mergedArray = [...firstArray, ...secondArray];
6
7// Remove Duplicate Items with Filter method
8const uniqueItemsWithFilter = mergedArray.filter((val, pos) => {
9 return mergedArray.indexOf(val) === pos; // Remove return and brackets if you wish
10});
11
12// ...or remove Duplicate Items with Set and Destructuring
13const uniqueItemsWithSet = [...new Set(mergedArray)];
14
15console.log(uniqueItemsWithFilter); // [2, 4, 6, 5, 11, 3]
16console.log(uniqueItemsWithSet); // [2, 4, 6, 5, 11, 3]
2021-01-26
1// JS Tip of the Day
2// Remove char from a string by position (using filter method)
3
4const removeCharFromString = (str, position) => {
5 return str.split('').filter((_, index) => index != position).join('');
6}
7
8console.log(removeCharFromString("Lorem Ipsum", 3)); //Lorm Ipsum
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.