Skip to content

Instantly share code, notes, and snippets.

@pebueno
Created February 27, 2024 16:28
Show Gist options
  • Save pebueno/e1a71038ef296ea3ca307696f7d991a1 to your computer and use it in GitHub Desktop.
Save pebueno/e1a71038ef296ea3ca307696f7d991a1 to your computer and use it in GitHub Desktop.
Optimized Data Transformation: Achieving O(N) Time Complexity
// The task is to implement generateOrderSummary function
const users = [
{ id: 1, name: 'Alice', age: 30 },
{ id: 2, name: 'Bob', age: 25 },
{ id: 3, name: 'Charlie', age: 35 }
];
const products = [
{ id: 101, name: 'Product A', price: 50 },
{ id: 102, name: 'Product B', price: 100 },
{ id: 103, name: 'Product C', price: 75 }
];
const orders = [
{ id: 1001, userId: 1, productId: 101, quantity: 2 },
{ id: 1002, userId: 2, productId: 102, quantity: 1 },
{ id: 1003, userId: 1, productId: 103, quantity: 3 }
];
const generateOrderSummary = ({ orders, products, users }) => {
// TODO: it should generate summary for every order
// so that response will look like this:
//{
// orderId: 1001,
// user: { id: 1, name: 'Alice', age: 30 },
// product: { id: 101, name: 'Product A', price: 50 },
// total: 100 // price * quantity
//}
const userMap = new Map (users.map(user => [user.id, user]));
const productMap = new Map (products.map(product => [product.id, product]));
return orders.map((order) => {
const user = userMap.get(order.userId);
const product = productMap.get(order.productId);
const total = product.price * order.quantity;
return {
orderId: order.id,
user: { id: user.id, name: user.name, age: user.age },
product: { id: product.id, name: product.name, price: product.price },
total: total
};
});
};
const Component = () => {
const [body, setBody] = React.useState({});
React.useEffect(() => {
try {
setBody(generateOrderSummary({ products, orders, users }))
} catch (err) {
console.log(err)
}
}, []);
return (
<div className="container">
<pre>{JSON.stringify(body, null, 2)}</pre>
</div>
)
}
ReactDOM.createRoot(document.getElementById('app')).render(<Component />);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment