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 arrayOneLevelDeep = [6,9,[6],6]; | |
| [].concat(...arrayOneLevelDeep); | |
| // or | |
| [].concat.apply([],arrayOneLevelDeep); | |
| // Returns | |
| [6, 9, 6, 6] |
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 someArray = [1, 2, , , [3, 4, [5, null, ["", 8, [undefined, 10]]]]]; | |
| someArray.flat(Infinity).filter(Boolean).map(Number); | |
| // or | |
| someArray.toString().split(",").filter(Boolean).map(Number); | |
| // Both returns | |
| [1, 2, 3, 4, 5, 8, 10] |
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 someArray = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]; | |
| // toString() | |
| someArray.toString().split(",").map(Number); | |
| // or you could use template literal which is faster than toString() | |
| `${someArray}`.split(",").map(Number); | |
| // Returns | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
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 arrayWithEmptySlots = [1, 2, , 3, , 4].flat(); | |
| //Returns | |
| [1, 2, 3, 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 someArray = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]; | |
| someArray.flat(Infinity); | |
| // Returns | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
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 monkeys =["๐",["๐","๐"],"๐"].flat(); | |
| // one above is same as the one below | |
| const monkeys = ["๐",["๐","๐"],"๐"].flat(1); | |
| // Returns | |
| [๐,๐,๐,๐] |
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
| // Long way | |
| if(array.indexOf(item) > -1) // Item found | |
| if(array.indexOf(item) === -1) // Item NOT found | |
| // Shorthand way ๐๐ฝ | |
| if(array.includes(item))// Item found | |
| if(!array.includes(item))// Item NOT found | |
| if(~arr.indexOf(item)) // Item found |
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
| let someString = "doubt-not"; | |
| someString = "false"; | |
| !!someString // returns true | |
| someString = "0"; | |
| !!someString //returns true | |
| let someString = undefined; | |
| !!someString //returns false |
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
| // Use this: | |
| Boolean(someString); // returns true, same as !!someString | |
| Boolean("0"); //returns true, in JavaScript, a non-empty string is always true. | |
| //Not this: | |
| const foo = new Boolean(bar); // returns a wrapper object |
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
| Number(someStringNum) + 2; // returns 5.5 | |
| Number(null); // returns 0 | |
| Number(""); // return NaN | |
| // Fast but messy | |
| someStringNum * 1 + 2; // returns 5.5 |
NewerOlder