JSPM

random-unique-subarray

2.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16
  • Score
    100M100P100Q41232F
  • License MIT

Get a random subarray of the specified length with unique elements

Package Exports

  • random-unique-subarray

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (random-unique-subarray) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

random-unique-subarray

https://nodei.co/npm/random-unique-subarray.png?downloads=true&downloadRank=true&stars=true
Get a random subarray of the specified length with unique elements

Install

npm install random-unique-subarray

Usage

import uniqueSubarr from 'random-unique-subarray';

const arr = [1, 2, 3, 4, 5]

console.log(uniqueSubarr(arr, 2))
// => [2, 5]

console.log(uniqueSubarr(arr, 4))
// => [5, 2, 4, 3]

API

uniqueSubarr(array, number, options?)

Returns a random subarray of the specified length with unique elements

array
Type: []
Description: Array of elements from which a subarray will be created

number
Type: Number
Description: Length a created subarray

options
Type: Object

options.removeDuplicates
Type: Boolean
Default: false
Description: Flag whether to delete the same elements in array

options.key
Type: String
Default: undefined
Description: if array is array of object. We can remove any duplicate values based on this key object.
More: https://github.com/tjcafferkey/removeduplicates

Examples

const arr = [1, 2, 2]

console.log(uniqueSubarr(arr, 2))
// => [2, 2] or [1, 2]

console.log(uniqueSubarr(arr, 2, {
      removeDuplicates: true
}))
// => [1, 2]
const arr = [{
    id: 1,
    name: 'Name1'
}, {
    id: 2,
    name: 'Name2'
}, {
    id: 2,
    name: 'Name2'
}]

console.log(uniqueSubarr(arr, 2, {
    removeDuplicates: true,
    key: 'id'
}))
// => [ { id: 2, name: 'Name2' }, { id: 1, name: 'Name1' } ]