@tpmjs/tools-dedupe-by-key
Remove duplicate objects from an array based on one or more key fields. For each unique key value, keeps either the first or last occurrence. Supports composite keys (multiple fields) and nested field access using dot notation.
Test @tpmjs/tools-dedupe-by-key (dedupeByKeyTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-dedupe-by-keypnpm add @tpmjs/tools-dedupe-by-keyyarn add @tpmjs/tools-dedupe-by-keybun add @tpmjs/tools-dedupe-by-keydeno add npm:@tpmjs/tools-dedupe-by-keyimport { dedupeByKeyTool } from '@tpmjs/tools-dedupe-by-key';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { dedupeByKeyTool } from '@tpmjs/tools-dedupe-by-key';
const result = await generateText({
model: openai('gpt-4o'),
tools: { dedupeByKeyTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
rowsarrayArray of objects to deduplicate
keyobjectField name(s) to use as unique key. Can be a single field name (string) or array of field names for composite keys. Supports dot notation for nested fields.
keepLastbooleanIf true, keeps the last occurrence of each duplicate. If false (default), keeps the first occurrence.
Schema extracted: 1/1/2026, 8:18:19 AM
Remove duplicate objects from an array based on one or more key fields.
npm install @tpmjs/tools-dedupe-by-key ai
import { dedupeByKeyTool } from '@tpmjs/tools-dedupe-by-key'; import { generateText } from 'ai'; const result = await generateText({ model: yourModel, tools: { dedupeByKey: dedupeByKeyTool, }, prompt: 'Remove duplicate users by email address', });
rows (array, required): Array of objects to deduplicatekey (string | string[], required): Field name(s) to use as unique key
"email"["firstName", "lastName"]"user.email" or ["user.id", "account.type"]keepLast (boolean, optional): If true, keeps last occurrence; if false (default), keeps first{ rows: Record<string, unknown>[], // Deduplicated array duplicatesRemoved: number, // Number of duplicates removed originalCount: number, // Original array length uniqueCount: number // Deduplicated array length }
const users = [ { id: 1, email: 'alice@example.com', name: 'Alice' }, { id: 2, email: 'bob@example.com', name: 'Bob' }, { id: 3, email: 'alice@example.com', name: 'Alice Updated' }, ]; // Keep first occurrence (default) // Returns: { rows: [Alice, Bob], duplicatesRemoved: 1, ... } await dedupeByKeyTool.execute({ rows: users, key: 'email', }); // Keep last occurrence // Returns: { rows: [Bob, Alice Updated], duplicatesRemoved: 1, ... } await dedupeByKeyTool.execute({ rows: users, key: 'email', keepLast: true, });
const events = [ { userId: 1, action: 'login', timestamp: '2024-01-01T10:00:00Z' }, { userId: 1, action: 'login', timestamp: '2024-01-01T10:05:00Z' }, { userId: 1, action: 'logout', timestamp: '2024-01-01T11:00:00Z' }, { userId: 2, action: 'login', timestamp: '2024-01-01T10:00:00Z' }, ]; // Dedupe by userId AND action // Returns: { rows: [user1-login, user1-logout, user2-login], duplicatesRemoved: 1, ... } await dedupeByKeyTool.execute({ rows: events, key: ['userId', 'action'], });
const orders = [ { id: 1, customer: { email: 'alice@example.com' }, total: 100 }, { id: 2, customer: { email: 'bob@example.com' }, total: 200 }, { id: 3, customer: { email: 'alice@example.com' }, total: 150 }, ]; // Dedupe by nested field // Returns: { rows: [order1, order2], duplicatesRemoved: 1, ... } await dedupeByKeyTool.execute({ rows: orders, key: 'customer.email', });
const stockPrices = [ { symbol: 'AAPL', price: 150.0, timestamp: '2024-01-01T09:00:00Z' }, { symbol: 'GOOGL', price: 140.0, timestamp: '2024-01-01T09:00:00Z' }, { symbol: 'AAPL', price: 152.0, timestamp: '2024-01-01T10:00:00Z' }, { symbol: 'AAPL', price: 151.0, timestamp: '2024-01-01T11:00:00Z' }, ]; // Get most recent price for each symbol // Returns: { rows: [GOOGL@140, AAPL@151], duplicatesRemoved: 2, ... } await dedupeByKeyTool.execute({ rows: stockPrices, key: 'symbol', keepLast: true, });
MIT
Downloads/month
0
Quality Score