1

I have DB (use json-server) like this:

DB = {
  products: [
    {id: 0, name: 'aaa', price: 10},
    {id: 1, name: 'bbb', price: 20},
    {id: 2, name: 'ccc', price: 50},
    {id: 3, name: 'ddd', price: 1}
  ]
};

And array of ids:

cartItemsIds: [0, 3];

How can I get array of objects from DB with ids from cartItemsIds ?

  • Does this answer your question? [How to filter an array from all elements of another array](https://stackoverflow.com/questions/34901593/how-to-filter-an-array-from-all-elements-of-another-array) – AZ_ May 02 '20 at 09:01
  • Not exactly. This method should be work, but I know that json-server has inline filter feature. So there's no need to load all db.products, but filter them on server side. – Eduard Virchenko May 02 '20 at 10:09

2 Answers2

1

Solved. If anyone needs:

`products?id=${cartItemsIds.join('&id=')}`
0

You can loop over your products from db like so:

for(const product of DB.products) {    
  if(cartItemsIds.includes(product.id)) {
   console.log(product);
  }
}
ionut-t
  • 1,121
  • 1
  • 7
  • 14