An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

[![Travis CI](https://travis-ci.com/scriptex/random-splice.svg?branch=master)](https://travis-ci.com/scriptex/random-splice)
[![Github Build](https://github.com/scriptex/random-splice/workflows/Build/badge.svg)](https://github.com/scriptex/random-splice/actions?query=workflow%3ABuild)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/34d3d75710534dc6a38c3584a1dcd068)](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)
[![Codebeat Badge](https://codebeat.co/badges/d765a4c8-2c0e-44f2-89c3-fa364fdc14e6)](https://codebeat.co/projects/github-com-scriptex-random-splice-master)
[![CodeFactor Badge](https://www.codefactor.io/repository/github/scriptex/random-splice/badge)](https://www.codefactor.io/repository/github/scriptex/random-splice)
[![DeepScan grade](https://deepscan.io/api/teams/3574/projects/5257/branches/40799/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=3574&pid=5257&bid=40799)
[![Analytics](https://ga-beacon-361907.ew.r.appspot.com/UA-83446952-1/github.com/scriptex/random-splice/README.md?pixel)](https://github.com/scriptex/random-splice/)

# Random Splice

> Get random element from array when looping

## Visitor stats

![GitHub stars](https://img.shields.io/github/stars/scriptex/random-splice?style=social)
![GitHub forks](https://img.shields.io/github/forks/scriptex/random-splice?style=social)
![GitHub watchers](https://img.shields.io/github/watchers/scriptex/random-splice?style=social)
![GitHub followers](https://img.shields.io/github/followers/scriptex?style=social)

## Code stats

![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/scriptex/random-splice)
![GitHub repo size](https://img.shields.io/github/repo-size/scriptex/random-splice?style=plastic)
![GitHub language count](https://img.shields.io/github/languages/count/scriptex/random-splice?style=plastic)
![GitHub top language](https://img.shields.io/github/languages/top/scriptex/random-splice?style=plastic)
![GitHub last commit](https://img.shields.io/github/last-commit/scriptex/random-splice?style=plastic)

## 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:






 



 



 



 



 



 



 



 



 



 



 



---


Support and sponsor my work: