Skip to content

Commit a75d822

Browse files
author
Giorgi Merabishvili
committed
shuffle-array
1 parent 7ec6649 commit a75d822

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/JavaScript-snippets.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
|36 | [reduceRight](#reduceRight)|
4343
|37 | [Abort Fetch](#Abort-Fetch)|
4444
|38 | [How to change the value of an object which is inside an array](#How-to-change-the-value-of-an-object-which-is-inside-an-array)|
45+
|39 | [Shuffle Array](#Shuffle-Array)|
4546

4647

4748

@@ -845,4 +846,17 @@ const newState = state.map((obj) =>
845846
846847
```
847848
849+
**[⬆ Back to Top](#table-of-contents)**
850+
### Shuffle Array
851+
852+
```javascript
853+
// Three quick steps to shuffle an array in Javascript: Map to a random number, sort on that and map the object back!
854+
shuffleArray = anArray =>
855+
anArray
856+
.map(a => [Math.random(), a])
857+
.sort((a, b) => a[0] - b[0])
858+
.map(a => a[1]);
848859
860+
console.log('Shuffled array', shuffleArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) // Returns shuffled array
861+
862+
```

0 commit comments

Comments
 (0)