Skip to content

sets

Variables

countBy

const countBy: typeof esToolkit_countBy = esToolkit_countBy;

Counts the occurrences of items in a Set based on a transformation function.

This function takes a Set and a function that generates a key from each value. It returns a Map with the generated keys and their counts as values. The count is incremented for each element for which the transformation produces the same key.

Template

The type of elements in the Set.

Template

The type of keys produced by the transformation function.

Param

The Set to count occurrences from.

Param

The function to produce a key for counting.

Returns

A Map containing the mapped keys and their counts.

Examples

const set = new Set([1, 2, 3, 4, 5]);
const result = countBy(set, (value) => value % 2 === 0 ? 'even' : 'odd');
// result will be Map(2) { 'odd' => 3, 'even' => 2 }
const set = new Set(['apple', 'banana', 'cherry']);
const result = countBy(set, (value) => value.length);
// result will be Map(2) { 5 => 1, 6 => 2 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


every

const every: typeof esToolkit_every = esToolkit_every;

Tests whether all elements in a Set satisfy the provided predicate function.

This function iterates through all elements of the Set and checks if the predicate function returns true for every element. It returns true if the predicate is satisfied for all elements, and false otherwise.

Template

The type of elements in the Set.

Param

The Set to test.

Param

A predicate function that tests each element.

Returns

true if all elements satisfy the predicate, false otherwise.

Example

const set = new Set([10, 20, 30]);
const result = every(set, (value) => value > 5);
// result will be: true

const result2 = every(set, (value) => value > 15);
// result2 will be: false

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


filter

const filter: typeof esToolkit_filter = esToolkit_filter;

Filters a Set based on a predicate function.

This function takes a Set and a predicate function, and returns a new Set containing only the elements for which the predicate function returns true.

Template

The type of elements in the Set.

Param

The Set to filter.

Param

A predicate function that tests each element.

Returns

A new Set containing only the elements that satisfy the predicate.

Example

const set = new Set([1, 2, 3, 4, 5]);
const result = filter(set, (value) => value > 2);
// result will be:
// Set(3) { 3, 4, 5 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


find

const find: typeof esToolkit_find = esToolkit_find;

Finds the first element in a Set for which the predicate function returns true.

This function iterates through the elements of the Set and returns the first element for which the predicate function returns true. If no element satisfies the predicate, it returns undefined.

Template

The type of elements in the Set.

Param

The Set to search.

Param

A predicate function that tests each element.

Returns

The first element that satisfies the predicate, or undefined if none found.

Example

const set = new Set([
  { name: 'apple', quantity: 10 },
  { name: 'banana', quantity: 5 },
  { name: 'grape', quantity: 15 }
]);
const result = find(set, (value) => value.quantity > 10);
// result will be: { name: 'grape', quantity: 15 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


keyBy

const keyBy: typeof esToolkit_keyBy = esToolkit_keyBy;

Maps each element of a Set based on a provided key-generating function.

This function takes a Set and a function that generates a key from each value. It returns a new Map where the keys are generated by the key function and the values are the corresponding values from the original set. If multiple elements produce the same key, the last value encountered will be used.

Template

The type of elements in the Set.

Template

The type of keys to produce in the returned Map.

Param

The set of elements to be mapped.

Param

A function that generates a key from a value.

Returns

A Map where the generated keys are mapped to each element's value.

Example

const set = new Set([
  { type: 'fruit', name: 'apple' },
  { type: 'fruit', name: 'banana' },
  { type: 'vegetable', name: 'carrot' }
]);
const result = keyBy(set, item => item.type);
// result will be:
// Map(2) {
//   'fruit' => { type: 'fruit', name: 'banana' },
//   'vegetable' => { type: 'vegetable', name: 'carrot' }
// }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


map

const map: typeof esToolkit_map = esToolkit_map;

Creates a new Set with elements transformed by the provided function.

This function takes a Set and a function that generates a new value from each element. It returns a new Set where the elements are the result of applying the function to each element.

Template

The type of elements in the input Set.

Template

The type of elements in the output Set.

Param

The Set to transform.

Param

A function that generates a new value from an element.

Returns

A new Set with transformed elements.

Example

const set = new Set([1, 2, 3]);
const result = map(set, (value) => value * 2);
// result will be:
// Set(3) { 2, 4, 6 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


reduce

const reduce: typeof esToolkit_reduce = esToolkit_reduce;

Reduces a Set to a single value by iterating through its elements and applying a callback function.

This function iterates through all elements of the Set and applies the callback function to each element, accumulating the result. If an initial value is provided, it is used as the starting accumulator value. If no initial value is provided and the Set is empty, a TypeError is thrown.

Template

The type of elements in the Set.

Template

The type of the accumulator.

Param

The Set to reduce.

Param

A function that processes each element and updates the accumulator.

Param

The initial value for the accumulator. If not provided, the first element in the Set is used.

Returns

The final accumulated value.

Throws

If the Set is empty and no initial value is provided.

Examples

const set = new Set([1, 2, 3]);
const result = reduce(set, (acc, value) => acc + value, 0);
// result will be: 6
const set = new Set([10, 20]);
const result = reduce(set, (acc, value) => acc + value);
// result will be: 30 (starts with first value 10)

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


some

const some: typeof esToolkit_some = esToolkit_some;

Tests whether at least one element in a Set satisfies the provided predicate function.

This function iterates through the elements of the Set and checks if the predicate function returns true for at least one element. It returns true if any element satisfies the predicate, and false otherwise.

Template

The type of elements in the Set.

Param

The Set to test.

Param

A predicate function that tests each element.

Returns

true if at least one element satisfies the predicate, false otherwise.

Example

const set = new Set([1, 2, 3]);
const result = some(set, (value) => value > 2);
// result will be: true

const result2 = some(set, (value) => value > 5);
// result2 will be: false

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit