简体   繁体   中英

List the products from an array based on ids from another object

I have the array

[
  {
    "id": "1",
    "Name": "Name 1",
    "Price": 10,
    "inventory": 1,
    "date_added": 1496757350
  },
  {
    "id": "2",
    "Name": "Name 2",
    "Price": 40,
    "inventory": 1,
    "date_added": 1496757290
  },
...
]

and the object

{
'1':2,
'2':15
'10':5
}

like this. So the first one contains the products, and the object contains the ids of the products (key), and how many times it has been added to the cart (value). What I want to achieve, is listing basically the products, in another array of objects, based on the object. So in this case would be 3 products in that list. I am a little lost here ... reduce, filter, map .... How should I proceed ? Thanks

If you want to list products available in another object (that uses product ids as keys) you'll need 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)) 

You could filter the products and build a new result set with the amount of cart.

 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('; '); })); 

You can use reduce to make a map of your products by id:

{ 
  id: { /* ... product */ }
}

You can use Object.keys and map to iterate over the ids in your cart:

Object.keys(cart).map(id => productMap[id])

This ensures you only loop over your product list once, instead of once for every item in the cart.

Also note that you might get more out of your question if you show us the attempts you've made. Are you aware of what reduce and map do?

 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); 

You can create an output object and add the item with the number of times it has been added.

 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); 

For speed reason, for lookup's etc into array, I tend to create a kind of index using another associated array. Something like below..

 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);
});

Something like that? The filter checks if the objects id exists within the keys of the quantity object and returns true or false depending on the result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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