From a75d822dc017a739a9ec902425db53189cc7b9ad Mon Sep 17 00:00:00 2001 From: Giorgi Merabishvili Date: Wed, 18 Nov 2020 21:59:10 +0400 Subject: [PATCH] shuffle-array --- .idea/.gitignore | 8 ++++++++ .idea/JavaScript-snippets.iml | 9 +++++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ README.md | 14 ++++++++++++++ 6 files changed, 51 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/JavaScript-snippets.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/JavaScript-snippets.iml b/.idea/JavaScript-snippets.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/JavaScript-snippets.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6cdd868 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 97ef1e7..e14f09c 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ |36 | [reduceRight](#reduceRight)| |37 | [Abort Fetch](#Abort-Fetch)| |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)| +|39 | [Shuffle Array](#Shuffle-Array)| @@ -845,4 +846,17 @@ const newState = state.map((obj) => ``` +**[⬆ Back to Top](#table-of-contents)** +### Shuffle Array + +```javascript +// Three quick steps to shuffle an array in Javascript: Map to a random number, sort on that and map the object back! +shuffleArray = anArray => + anArray + .map(a => [Math.random(), a]) + .sort((a, b) => a[0] - b[0]) + .map(a => a[1]); +console.log('Shuffled array', shuffleArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) // Returns shuffled array + +```