Socket
Book a DemoInstallSign in
Socket

random-splice

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

random-splice

Get random element from array when looping

latest
Source
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

Travis CI Github Build Codacy Badge Codebeat Badge CodeFactor Badge DeepScan grade Analytics

Random Splice

Get random element from array when looping

Visitor stats

GitHub stars GitHub forks GitHub watchers GitHub followers

Code stats

GitHub code size in bytes GitHub repo size GitHub language count GitHub top language GitHub last commit

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

yarn add random-splice

or

npm i random-splice

Usage

import { randomSplice } from 'random-splice';

const arr = ['Your', 'Awesome', 'Array'];

while (arr.length) {
	console.log(randomSplice(arr));
}

Notes

  • This function modifies the original array. This is where its magic comes from
  • This function accepts only arrays
  • This function can be used in an ES6 environment

LICENSE

MIT

Connect with me:

Support and sponsor my work:

Keywords

Loop

FAQs

Package last updated on 07 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts