Iterates nested loops and concatenates values onto an Array or Set.
Requires Node.js 8.3.0 or above.
npm i concat-each
The module exports a single function.
base
(Array, Set, WeakSet): The collection onto which new items should be concatenated. If you want to create a new collection, pass in[]
ornew Set()
ornew WeakSet()
.- Variadic:
...iterables
(one or more of: iterable): Iterable collections of items to pass tocb
. Specify multiple iterables if you want nested loops. cb
(function): A callback which accepts an argument for each iterable initerables
and returns an array of items to concatenate ontobase
.- Optional: Object argument:
arrays
/sets
/weakSets
(arrays of classes/strings): Arrays of classes and/or string names of classes that should be treated as equivalent toArray
/Set
/WeakSet
(respectively).loose
(boolean): Whether or not to compare values loosely (as defined bylooselyEquals
) for the purpose of testing uniqueness ifunique
istrue
. Defaults tofalse
.looselyEquals
(function): A callback that accepts two values and returnstrue
if they are to be considered equivalent orfalse
otherwise. This argument is only used ifloose
istrue
. If omitted, the default behavior will, among other things, consider arrays/objects to be equal if they have the same entries.unique
(boolean): Whether or not to refrain from adding values that already exist inbase
. Defaults tofalse
. You can define what uniqueness means by using theloose
andlooselyEquals
arguments.
Modifies and returns base
.
Explaining the module is best done by showing the code it replaces:
const n = []
for (const i of [1, 2, 3]) {
for (const j of [2, 3, 4]) {
const sum = i + j
if (sum % 2 === 0) n.push(sum)
}
}
n // [4, 4, 6, 6]
const concatEach = require('concat-each')
const n = concatEach([], [1, 2, 3], [2, 3, 4], (i, j) => (i + j) % 2 === 0 ? [i + j] : []) // [4, 4, 6, 6]
- neach: Nested forEach