简体   繁体   English

用于 Javascript 的类似 SQL 的内部联接

[英]SQL-like Inner Join for Javascript

 const products = [{ id: 5, productName: "Logitech Mouse", unitprice: 35 }, { id: 6, productName: "Logitech Keyboard", unitprice: 40 } ]; const cart = [{ id: 101, userId: 3, productId: 5, quantity: 2 }]; totals = cart.reduce((r, { productId: id, quantity }) => (r[id] = (r[id] || 0) + quantity, r), {}), result = products.map(({ id, productName, unitprice }) => ({ productName, unitprice, quantity: totals[id] })); console.log(result)

Currently, it prints two rows instead of one row.目前,它打印两行而不是一行。 Second row should not be printed because there is only one row in cart .不应打印第二行,因为cart只有一行。 How do I the so-called inner join which return only one record which is for cart id 101?我如何进行所谓的内部连接,它只返回一个用于购物车 ID 101 的记录?

If you want retain the cart with the "join", you can map the found product (without its id) and include the quantity.如果您想保留带有“join”的购物车,您可以映射找到的产品(没有其 id)并包括数量。

 const products = [{ id: 5, productName: "Logitech Mouse", unitprice: 35 }, { id: 6, productName: "Logitech Keyboard", unitprice: 40 }]; const cart = [{ id: 101, userId: 3, productId: 5, quantity: 2 }]; let joined = cart.map(item => { let { id, ...rest} = products.find(p => p.id === item.productId); return { ...rest, 'quantity' : item.quantity }; }); console.log(joined);
 .as-console-wrapper { top: 0; max-height: 100% !important; }


If you want the total price of all items in the cart you will need to:如果您想了解购物车中所有商品的总价,您需要:

  1. Reduce the cart by the items within it通过其中的项目减少购物车
  2. Locate the product by its id通过 id 定位产品
  3. Add to the total, the unit price of the product times the quantity in the cart加总,产品的单价乘以购物车中的数量

 const products = [{ id: 5, productName: "Logitech Mouse", unitprice: 35 }, { id: 6, productName: "Logitech Keyboard", unitprice: 40 }]; const cart = [{ id: 101, userId: 3, productId: 5, quantity: 2 }]; let total = cart.reduce((subtotal, item) => { let product = products.find(p => p.id === item.productId); return subtotal + product.unitprice * item.quantity; }, 0); console.log(total);

Short version:精简版:

let t = cart.reduce((s, i) => s + products.find(p => p.id === i.productId).unitprice * i.quantity, 0);

I recommend to use map function like this我建议使用这样的map功能

 const products = [{ id: 5, productName: "Logitech Mouse", unitprice: 35 }, { id: 6, productName: "Logitech Keyboard", unitprice: 40 } ]; const cart = [{ id: 101, userId: 3, productId: 5, quantity: 2 }]; result = cart.map(item => { const product = products.find(product => item.productId === product.id); return { id: item.id, quantity: item.quantity, productName: product.productName, unitprice: product.unitprice } }); console.log(result);

While Mr. Polywhirl's answer is a cleaner and better solution, here is a simple edit to your code that solves your problem.虽然 Polywhirl 先生的答案是一个更清晰、更好的解决方案,但这里对您的代码进行了简单的编辑,可以解决您的问题。 The reason why your code returns two is because of the map .您的代码返回两个的原因是因为map Replace it with filter and the condition on the presence of id to solve the issue.将其替换为filter和存在id的条件以解决问题。

 const products = [{ id: 5, productName: "Logitech Mouse", unitprice: 35 }, { id: 6, productName: "Logitech Keyboard", unitprice: 40 } ]; const cart = [{ id: 101, userId: 3, productId: 5, quantity: 2 }]; totals = cart.reduce((r, { productId: id, quantity }) => (r[id] = (r[id] || 0) + quantity, r), {}), result = products.filter(({ id, productName, unitprice }) => { if (totals[id]) return ({ productName, unitprice, quantity: totals[id] }) }); console.log(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM