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 items = [1, 2, 3, 4]; | |
| //before | |
| items.sort(() => Math.random() - 0.5); | |
| //after | |
| items.shuffle(); |
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 items = [1, 2, 3, 4]; | |
| //before | |
| const index = items.reverse().findIndex(fn); // the reverse method mutates the array | |
| //after | |
| const index = items.findLastIndex(fn); |
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 items = [1, 2, 3, 4]; | |
| //before | |
| const item = items.reverse().find(fn); // the reverse method mutates the array | |
| //after | |
| const item = items.findLast(fn); |
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 items = [1, 2, 3, 4]; | |
| //before | |
| const itemsForDelete = items.filter(item => item % 2 === 0); | |
| itemsForDelete.forEach(item => { | |
| const index = items.indexOf(item); | |
| items.splice(index, 1); | |
| }) | |
| //after | |
| items.deleteWhere(item => item % 2 === 0); |
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 items = [1, 2, 3, 4]; | |
| //before | |
| items.splice(index, 1); | |
| //after | |
| items.deleteAt(index); |
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 items = [1, 2, 3, 4]; | |
| //before | |
| const index = items.indexOf(item); | |
| items.splice(index, 1); | |
| //after | |
| items.delete(item); |
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 items = [1, 2, 3, 4]; | |
| //before | |
| items.splice(1, 0, 3); | |
| console.log(items); | |
| //after | |
| items.insert(1, 3); | |
| console.log(items); |
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 items = [1, 2, 3, 4]; | |
| //before | |
| const two = items[1]; | |
| const three = items[2]; | |
| //after | |
| const two = items.get(1); | |
| const three = items.get(2); |
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 items = [1, 2, 3, 4]; | |
| items.clear(); | |
| console.log(items); |
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 items = [1, 2, 3, 4]; | |
| items.splice(0, items.length); | |
| //or | |
| while(items.length > 0) { | |
| items.pop(); | |
| } |