https://github.com/scriptex/random-splice
Get random element from array
https://github.com/scriptex/random-splice
array-manipulations randomizer
Last synced: 7 months ago
JSON representation
Get random element from array
- Host: GitHub
- URL: https://github.com/scriptex/random-splice
- Owner: scriptex
- License: mit
- Created: 2018-03-09T09:28:14.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2025-04-13T04:54:54.000Z (9 months ago)
- Last Synced: 2025-04-13T05:28:46.758Z (9 months ago)
- Topics: array-manipulations, randomizer
- Language: JavaScript
- Homepage: https://atanas.info/portfolio/open-source/random-splice
- Size: 1.12 MB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.com/scriptex/random-splice)
[](https://github.com/scriptex/random-splice/actions?query=workflow%3ABuild)
[](https://www.codacy.com/gh/scriptex/random-splice/dashboard?utm_source=github.com&utm_medium=referral&utm_content=scriptex/random-splice&utm_campaign=Badge_Grade)
[](https://codebeat.co/projects/github-com-scriptex-random-splice-master)
[](https://www.codefactor.io/repository/github/scriptex/random-splice)
[](https://deepscan.io/dashboard#view=project&tid=3574&pid=5257&bid=40799)
[](https://github.com/scriptex/random-splice/)
# Random Splice
> Get random element from array when looping
## Visitor stats




## Code stats





## Idea
When looping over an array in JavaScript, you sometimes want to get a random item.
One solution is to first shuffle the array and then loop over it.
Another one is to get random element while looping.
## Use case
Lets say that you want to render a word cloud.
Lets say that you do not want to keep the same order each time.
This is where you can use the `randomSplice` function.
## Installation
```sh
yarn add random-splice
```
or
```sh
npm i random-splice
```
## Usage
```javascript
import { randomSplice } from 'random-splice';
const arr = ['Your', 'Awesome', 'Array'];
while (arr.length) {
console.log(randomSplice(arr));
}
```
## Notes
1. This function modifies the original array. This is where its magic comes from
2. This function accepts only arrays
3. This function can be used in an ES6 environment
## LICENSE
MIT
---
Connect with me:
---