![](/img/trans.png)
[英]How to select only products with ids from ids array. MongoDB
[英]List the products from an array based on ids from another object
我有陣列
[
{
"id": "1",
"Name": "Name 1",
"Price": 10,
"inventory": 1,
"date_added": 1496757350
},
{
"id": "2",
"Name": "Name 2",
"Price": 40,
"inventory": 1,
"date_added": 1496757290
},
...
]
和對象
{
'1':2,
'2':15
'10':5
}
像這樣。 因此,第一個包含產品,對象包含產品的ID(鍵)以及將其添加到購物車的次數(值)。 我要實現的是,在基於對象的另一個對象數組中,基本上列出產品。 因此,在這種情況下,該列表中將有3個產品。 我在這里有點迷路...縮小,過濾,映射....我應該如何進行? 謝謝
如果要列出另一個對象中可用的產品(使用產品ID作為鍵),則需要filter
const products = [ { "id": "1", "Name": "Name 1", "Price": 10, "inventory": 1, "date_added": 1496757350 }, { "id": "2", "Name": "Name 2", "Price": 40, "inventory": 1, "date_added": 1496757290 }, ] const ids = { '1': 2 } console.log(products.filter(product => product.id in ids))
您可以過濾產品並使用購物車數量構建新的結果集。
var products = [{ id: "1", Name: "Name 1", Price: 10, inventory: 1, date_added: 1496757350 }, { id: "2", Name: "Name 2", Price: 40, inventory: 1, date_added: 1496757290 }, { id: "4", Name: "Name 4", Price: 30, inventory: 1, date_added: 1496757290 }], cart = { 1: 2, 2: 15, 10: 5 }, selectedProducts = products.filter(function (o) { return o.id in cart; }); console.log(selectedProducts.map(function (o) { return ['id', 'Name'].map(function (k) { return k + ': ' + o[k]; }).concat('count: ' + cart[o.id]).join('; '); }));
您可以使用reduce
通過id制作產品地圖:
{
id: { /* ... product */ }
}
您可以使用Object.keys
並map
來迭代購物車中的ID:
Object.keys(cart).map(id => productMap[id])
這樣可確保您僅循環瀏覽商品列表一次,而不是購物車中的每個商品一次。
另請注意,如果您向我們展示您的嘗試,您可能會從中得到更多的幫助。 您知道reduce
和map
做什么嗎?
const products = [ { "id": "1", "Name": "Name 1", "Price": 10, "inventory": 1, "date_added": 1496757350 }, { "id": "2", "Name": "Name 2", "Price": 40, "inventory": 1, "date_added": 1496757290 }, { "id": "10", "Name": "Name 10", "Price": 40, "inventory": 1, "date_added": 1496757290 } ] const cart = { "1": 2, "2": 15, "10": 5 }; const productsById = products .reduce((map, prod) => Object.assign(map, { [prod.id]: prod }), {}); const list = Object .keys(cart) // Here, you can make any combination you like // since both data are retrievable by id // Only products: //.map(id => productsById[id]); // Combination .map(id => ({ product: productsById[id], quantity: cart[id] })); console.log(list);
您可以創建一個輸出對象,並添加該項目的次數。
var items = [{ "id": "1", "Name": "Name 1", "Price": 10, "inventory": 1, "date_added": 1496757350 }, { "id": "2", "Name": "Name 2", "Price": 40, "inventory": 1, "date_added": 1496757290 }]; var cart = { '1': 2, '2': 15, '10': 5 } var output = {}; for (id in cart) { item = items.find(x => x.id === id); if (item != null) output[id] = { item: item, count: cart[id] }; } console.log(output);
出於速度原因,為了在數組中查找等,我傾向於使用另一個關聯的數組來創建一種索引。 像下面這樣。
var products = [ { "id": "1", "Name": "Name 1", "Price": 10, "inventory": 1, "date_added": 1496757350 }, { "id": "2", "Name": "Name 2", "Price": 40, "inventory": 1, "date_added": 1496757290 } ]; var basket = { '1':2, '2':15 }; //first lets create a kind of index to products.. var ixProducts = products.reduce((a, v) => { a[v.id] = v; return a; }, {}); Object.keys(basket).forEach((k) => { console.log({ qty: basket[k], product: ixProducts[k] }); });
let result = products.filter(function( obj ) {
return (Object.keys(quantityObject).indexOf(obj.id) !== -1);
});
這樣的事嗎? 過濾器檢查對象id是否存在於數量對象的鍵中,並根據結果返回true或false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.