Array of primary object data or string or number
Array of foreign object data
mapping options
Array of primary data with foreign data
const primaryArray = [
{ id: 1, userId: 1 },
{ id: 2, userId: 2 },
];
const userArray = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Mary' },
];
const result = mapping(
primaryArray,
userArray,
{ foreignKey: 'id', primaryKey: 'userId', asField: 'user' },
);
expect(result).toEqual([
{ id: 1, user: { id: 1, name: 'John' }, userId: 1 },
{ id: 2, user: { id: 2, name: 'Mary' }, userId: 2 },
]);
const primaryArray = [
{ id: 1, users: [1, 2] },
];
const userArray = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Mary' },
];
const result = mapping(
primaryArray,
userArray,
{ foreignKey: 'id', primaryKey: 'users' },
);
expect(result).toEqual([
{ id: 1, users: [{ id: 1, name: 'John' }, { id: 2, name: 'Mary' }] },
]);
const primaryArray = [1, 2];
const userArray = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Mary' },
];
const result = mapping(
primaryArray,
userArray,
{ foreignKey: 'id' },
);
expect(result).toEqual([
{ id: 1, name: 'John' },
{ id: 2, name: 'Mary' },
]);
Generated using TypeDoc
Mapping foreign key value to primary key value
This functionality is almost similar to mongodb lookup https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/