This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const objectList = [ | |
| { value: 1 }, | |
| { value: 2 }, | |
| { value: 3 }, | |
| { value: 4 }, | |
| { value: 5 } | |
| ]; | |
| const maxValue = Math.max(...objectList.map(x => x.value)); | |
| // maxValue -> 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const start = 0; | |
| const end = 100; | |
| const step = 10; | |
| const arrayLength = Math.floor(((end - start) / step)) + 1; | |
| const range = [...Array(arrayLength).keys()].map(x => (x * step) + start); | |
| // [0,10,20,30,40,50,60,70,80,90,100] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const obj = { id: 1, name: 'laptop', price: 1200 }; | |
| Object.entries(obj).forEach(([key, value]) => { | |
| console.log(key, value); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const condition1 = true; | |
| const condition2 = false; | |
| const person = { | |
| id: 'uuid-1234', | |
| name: 'John', | |
| ...(condition1 && { propA: 'A' }), | |
| ...(condition2 && { propB: 'B' }), | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const randomItem = <T>(arr: T[]): T => arr[(Math.random() * arr.length) | 0]; | |
| randomItem(<number[]>[1, 2, 3, 4, 5]); // -> 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const removeTrailingSlash = (value: string): string => value && value.charAt(value.length - 1) === '/' ? value.slice(0, -1) : value; | |
| removeTrailingSlash('foo-bar/'); // -> foo-bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const randomHexColor = (): string => `#${Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, '0')}`; | |
| randomHexColor(); // -> #dc7c40 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const numberToLetter = (value: number): string => String.fromCharCode(94 + value); | |
| numberToLetter(4); // -> b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const randomNumberString = (): string => new Date().getTime() + Math.random().toString(36).slice(2); | |
| randomNumberString(); // -> 1646617484381wml196a8iso |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const randomInteger = (): number => new Date().getTime(); | |
| randomInteger(); // -> 1646617367345 |