const enrichEvents = (events) => {
const metadata = {
usr_789: { tier: "premium", joined: "2023-04-01" },
};
const enriched = events.map(event =>
{
id: event.id,
time: event.time,
user: metadata[event.userId],
value: event.properties.totalValue
}
);
return enriched;
};
const events = [
{
id: "evt_123",
time: "2024-12-10T10:00:00Z",
userId: "usr_789",
properties: {
totalValue: 99.99,
},
},
];
console.log(enrichEvents(events));
Most likely your IDE would catch this one for you, but there is a syntax error. The map function is an arrow function that implicitly returns the result of the expression inside the block. In this case, the block is not a valid expression, so the code will throw a syntax error. To fix this, you can wrap the object in parentheses to make it an expression.
const enrichEvents = (events) => {
const metadata = {
usr_789: { tier: "premium", joined: "2023-04-01" },
};
const enriched = events.map((event) => ({
id: event.id,
time: event.time,
user: metadata[event.userId],
value: event.properties.totalValue,
}));
return enriched;
};
const events = [
{
id: "evt_123",
time: "2024-12-10T10:00:00Z",
userId: "usr_789",
properties: {
totalValue: 99.99,
},
},
];
console.log(enrichEvents(events));