By default, JSON.stringify is used to create a string representing value. These are added
to a Set of strings, which is how we keep track of uniqueness. If the value is already a string it is used as-is.
This allows you to have custom logic for what determines uniqueness. Eg, using a single field
of an object as an identifier:
constpeople = [ { name:`Mary`, size:20 }, { name:`Abdul`, size:19 }, { name:`Mary`, size:5 } ] for (constvofuniqueByValue(d, v=>v.name)) { // Yields: { name: `Mary`, size: 20 }, { name: `Abdul`, size: 19 } // Second 'Mary' is skipped because name is the same, even though size field is different. }
If you want to keep track of the set of keys, or prime it with some existing data, provide a Set instance:
constunique = newSet(); unique.add(`b`); constd = [`a`, `b`, `c`]; for (constvofuniqueByValue(d, toStringDefault, unique)) { // Yields: `a`, `c` // `b` is skipped because it was already in set } // After completion, `unique` contains `a`, `b` and `c`.
Creating your own Set is useful for tracking unique values across several calls to uniqueByValue.
Filters the
input
iterable, only yielding unique values. Use unique to compare by object reference instead.Streaming: Works with unbounded iterables.
By default, JSON.stringify is used to create a string representing value. These are added to a Set of strings, which is how we keep track of uniqueness. If the value is already a string it is used as-is.
This allows you to have custom logic for what determines uniqueness. Eg, using a single field of an object as an identifier:
If you want to keep track of the set of keys, or prime it with some existing data, provide a Set instance:
Creating your own Set is useful for tracking unique values across several calls to
uniqueByValue
.Param: input
Param: seen
Param: toString