# Enumerating Arrays in JavaScript

###
Ashwin Kandel
*Updated on *
・2 min read

Enumeration is a process of arranging or selecting elements from an array in a certain way. They are also called permutation and combination. Let's look at several ways of enumerating an array in JavaScript

## Enumerating all Arrangement

Suppose there are **k** positions to be filled and **n** elements available in an array to fill these positions. Each position can take any values from **n** elements. Then, there are **n ^{k}** different ways of filling the positions. Let's look at ways of evaluating result of this kind of enumeration.

```
var enumAll = require('enum-all')
var arr = ['a','b','c','1','2','3']
//Generate all three letter password using characters from arr
var output = enumAll(arr, 3)
console.log(output) //['aaa', 'baa', 'caa' .... 213 more items]
```

## Enumerating Power Set

Power set is a set containing all possible sub-set of elements in an array. That is, given an array containing **n** elements its powerset contains all possible combinations of elements of various sizes.The size of powerset is **2 ^{n}** and let's look at ways of evaluating powerset of elements in an array.

```
var powerSet = require('enum-powerset')
var arr = ['Alice', 'Bob', 'Eve']
//generate powerset of above elements
var output = powerSet(arr)
// Prints the powerset of three elements in arr
console.log(output)
// [ [''],
// ['Alice'],
// ['Bob'],
// ['Alice,Bob'],
// ['Eve'],
// ['Alice,Eve]',
// ['Bob,Eve'],
// ['Alice,Bob,Eve' ]
// ]
```

## Enumerating certain combination

Power set contains all possible combinations of elements of various sizes. Suppose you just want combinations of size **k** from **n** elements then, this kind of enumeration comes in handy. There are total **nc _{k}** ways of choosing k elements from n elements which equals n!/(k!*(n-k)!) where

**!**refers to factorial. Let's look at an example of this.

```
var nck = require('enum-nck')
var teams =['united', 'city', 'liverpool']
//generate all possible football matches between above teams
var output = nck(teams, 2)
//prints the result
console.log(output)
//[ [ 'united', 'city' ],
// [ 'united', 'liverpool' ],
// [ 'city', 'liverpool' ],
//]
```

## Enumerating permutation

Permutation is a way of evaluating all different ways **n** elements of an array can be arranged. There are **n!(factorial)** ways of permutating elements in array if each element is unique. Let's look at way of evaluating permutation of elements.

```
var permutate = require('enum-permutate')
var arr = ['0','1','2']
//generate permutation of above elements
var output = permutate(arr)
//prints output
console.log(output)
//[ [ '0', '1', '2' ],
// [ '0', '2', '1' ],
// [ '1', '0', '2' ],
// [ '1', '2', '0' ],
// [ '2', '0', '1' ],
// [ '2', '1', '0' ]
//]
```

All the above example works in node.js as well as browser (through bundler such as browserify).

Can I suggest you include the output of your console.log's. It will make it easier for the reader to understand what the code is doing.

Yea , I think i should do that to make things clear. Thanks for the suggestion!