mergeByKey
KIT · function · Runtime value
Merge duplicates without moving their first position; selection policy remains host-owned.
The first occurrence of a key fixes the output position; select decides which item that position finally holds, so callers can prefer the newest or the most complete duplicate.
Import
ts
import { mergeByKey } from '@jangts/agent-core/kits/aggregation';js
import { mergeByKey } from '@jangts/agent-core/kits/aggregation';Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/aggregation, @jangts/agent-core/kits.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Item | — | — |
Key | — | — |
Call
ts
mergeByKey<Item, Key>(items: Iterable<Item>, key: (item: Item) => Key, select: (item: Item, previous: Item | undefined) => Item): Item[]Merge duplicates without moving their first position; selection policy remains host-owned.
The first occurrence of a key fixes the output position; select decides which item that position finally holds, so callers can prefer the newest or the most complete duplicate.
| Type parameter | Constraint | Default type |
|---|---|---|
Item | — | — |
Key | — | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
items | Iterable<Item> | Required | — |
key | (item: Item) => Key | Required | — |
select | (item: Item, previous: Item | undefined) => Item | Required | — |
Returns: Item[].
Examples
- Aggregation: query aggregation and stable merging: Read App data in order and keep how duplicate items are merged in the same configuration.
Aggregate two reads in order
Beginner · Node.js
ts
import { Aggregation } from '@jangts/agent-core/kits/aggregation';
const pages = new Map([[1, [{ id: 'a', title: '第一页' }]], [2, [{ id: 'a', title: '重复项' }, { id: 'b', title: '第二页' }]]]);
const query = Aggregation.defineQueryAggregation({ execute: (page: number) => pages.get(page) ?? [] });
console.log(query.collect([1, 2], undefined));js
import { Aggregation } from '@jangts/agent-core/kits/aggregation';
const pages = new Map([[1, [{ id: 'a', title: '第一页' }]], [2, [{ id: 'a', title: '重复项' }, { id: 'b', title: '第二页' }]]]);
const query = Aggregation.defineQueryAggregation({ execute: (page) => pages.get(page) ?? [] });
console.log(query.collect([1, 2], undefined));Add a primary key and retention policy to the same collect
Configuration · Node.js
ts
import { Aggregation } from '@jangts/agent-core/kits/aggregation';
type Row = { id: string; title: string };
const pages = new Map<number, Row[]>([[1, [{ id: 'a', title: '第一页' }]], [2, [{ id: 'a', title: '重复项' }, { id: 'b', title: '第二页' }]]]);
const query = Aggregation.defineQueryAggregation({
execute: (page: number) => pages.get(page) ?? [],
key: (row: Row) => row.id,
merge: (row: Row, previous: Row | undefined) => previous ?? row,
});
console.log(query.collect([1, 2], undefined));
console.log(Aggregation.mergeByKey([...pages.values()].flat(), row => row.id, (row, previous) => previous ?? row));js
import { Aggregation } from '@jangts/agent-core/kits/aggregation';
const pages = new Map([[1, [{ id: 'a', title: '第一页' }]], [2, [{ id: 'a', title: '重复项' }, { id: 'b', title: '第二页' }]]]);
const query = Aggregation.defineQueryAggregation({
execute: (page) => pages.get(page) ?? [],
key: (row) => row.id,
merge: (row, previous) => previous ?? row,
});
console.log(query.collect([1, 2], undefined));
console.log(Aggregation.mergeByKey([...pages.values()].flat(), row => row.id, (row, previous) => previous ?? row));