Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/firstandthird/combinations
Node library to find all combinations from an array
https://github.com/firstandthird/combinations
Last synced: 3 days ago
JSON representation
Node library to find all combinations from an array
- Host: GitHub
- URL: https://github.com/firstandthird/combinations
- Owner: firstandthird
- License: mit
- Created: 2012-08-12T21:48:27.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2023-05-16T16:57:57.000Z (over 1 year ago)
- Last Synced: 2024-08-09T08:26:21.382Z (6 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 23
- Watchers: 5
- Forks: 8
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Combinations [![Build Status](https://travis-ci.org/jgallen23/combinations.svg?branch=master)](https://travis-ci.org/jgallen23/combinations)
============# Installation
npm install combinations
# Usage
combinations(array, [min_output_array_size], [max])
* takes in an array, and outputs an array of arrays, containing all possible combinations of values in the original array.
* combinations are of all sizes: all combinations of one element, and all combinations of 2 elements, and so on
* minimum number of elements in a combination can be specified (min_output_array_size)
* maximum number of elements in a combination can be also specified
* if maximum combination value is bigger than array elements it will be overridden to the array length# Example
var combinations = require('combinations');
var myArray = ['red', 'orange', 'yellow', 'green'];
combinations(myArray);
//Output:
// [ [ 'red' ], [ 'orange' ],
// [ 'yellow' ], [ 'green' ],
// [ 'red', 'orange' ], [ 'red', 'yellow' ],
// [ 'red', 'green' ], [ 'orange', 'yellow' ],
// [ 'orange', 'green' ], [ 'yellow', 'green' ],
// [ 'red', 'orange', 'yellow' ], [ 'red', 'orange', 'green' ],
// [ 'red', 'yellow', 'green' ], [ 'orange', 'yellow', 'green' ],
// [ 'red', 'orange', 'yellow', 'green' ] ]# Example with a minimum array size
combinations(myArray, 2);
//Output:
// [ [ 'red', 'orange' ], [ 'red', 'yellow' ],
// [ 'red', 'green' ], [ 'orange', 'yellow' ],
// [ 'orange', 'green' ], [ 'yellow', 'green' ],
// [ 'red', 'orange', 'yellow' ], [ 'red', 'orange', 'green' ],
// [ 'red', 'yellow', 'green' ], [ 'orange', 'yellow', 'green' ],
// [ 'red', 'orange', 'yellow', 'green' ] ]# Example with a minimum and maximum array size
combinations(myArray, 2, 3);
//Output:
// [ [ 'red', 'orange' ], [ 'red', 'yellow' ],
// [ 'red', 'green' ], [ 'orange', 'yellow' ],
// [ 'orange', 'green' ], [ 'yellow', 'green' ],
// [ 'red', 'orange', 'yellow' ], [ 'red', 'orange', 'green' ],
// [ 'red', 'yellow', 'green' ], [ 'orange', 'yellow', 'green' ] ]