Gabriel Valente
Gabriel Valente

Reputation: 101

Is there a way to filter an item in a array inside another array

Basicaly I have an object that has an array in it and I have a superior array of this object.

The object is the following.

category: {
   name,
   items: [{
      id,
      name,
      price,
      image,
   }],
}

This array can look like:

[{
   name: "Category 1",
   items: [{
      id: 1,
      name: "Item 1 of category 1",
      price: 12.34,
      image: "/path/to/image",
   },
   {
      id: 2,
      name: "Item 2 of category 1",
      price: 56.78,
      image: "/path/to/image2",
   }]
},
{
   name: "Category 2",
   items: [{
      id: 3,
      name: "Item 1 of category 2",
      price: 87.65,
      image: "/path/to/image3",
   },
   {
      id: 4,
      name: "Item 2 of category 1",
      price: 43.21,
      image: "/path/to/image4",
   }]
}]

My question is, it's possible to search for the id since I have an array with all of this data.

Currently I am solving the problem with the following code:

var price = 0;
const menu = [];
state.user.menu.map((item, k) => {
  item.category.items.forEach((itm) => menu.push(itm));

  return null;
});

state.user.items.forEach((item) => {
  price += menu.filter((itm) => itm.id === item.id)[0].price * item.quantity;
});

This basicaly copies every item in the array inside of the object and ignores the category name so I only have a big array.

For what I need now, I need to correlate each item with the category name, so, I can't do as it is now. Basically, I have a list of Ids and I need to display them with the corresponding category that is in this array.

(Items to search)
[{
   timestamp: "123456",
   userid: "123456",
   ...
   id: 1,
   price: 12.34,
},
{
   timestamp: "123456",
   userid: "123456",
   ...
   id: 3,
   price: 87.65,
},
{
   timestamp: "123456",
   userid: "123456",
   ...
   id: 4,
   price: 43.21,
}]

(Expected Result)
[{
   name: "Category 1",
   items: [{
      id: 1,
      name: "Item 1 of category 1",
      price: 12.34,
      image: "/path/to/image",
   }]
},
{
   name: "Category 2",
   items: [{
      id: 3,
      name: "Item 1 of category 2",
      price: 87.65,
      image: "/path/to/image3",
   },
   {
      id: 4,
      name: "Item 2 of category 1",
      price: 43.21,
      image: "/path/to/image4",
   }]
}]

Any sugestions is welcome, thanks.

Upvotes: 1

Views: 99

Answers (2)

Majed Badawi
Majed Badawi

Reputation: 28434

const data = [
  { name: "Category 1", items: [{ id: 1, name: "Item 1 of category 1", price: 12.34, image: "/path/to/image" }, { id: 2, name: "Item 2 of category 1", price: 56.78, image: "/path/to/image2" }] },
  { name: "Category 2", items: [{ id: 3,  name: "Item 1 of category 2",  price: 87.65, image: "/path/to/image3" }, { id: 4, name: "Item 2 of category 1", price: 43.21, image: "/path/to/image4" }] }
];

const getItemsById = (arr = []) => {
  // get list of ids to search for
  const ids = arr.map(({ id }) => id);
  // iterate over list of objects
  return data.reduce((list, elem) => {
    // get current items with ids
    const items = elem.items.filter(({ id }) => ids.includes(id));
    // if any found, add element with filtered list
    if(items.length > 0) list.push({ ...elem, items });
    return list;
  }, []);
}

console.log( getItemsById([{ id: 1 }, { id: 3 }, { id: 4 }]) );

Upvotes: 1

ahsan
ahsan

Reputation: 1504

Is this what you're looking for?

const obj = {
  category: {
     name: "Test",
     items: [{
        id: 1,
        name: "Test",
        price: 50,
        image: "Test",
     }],
  }
}

console.log(obj.category.items[0].id);

Upvotes: 0

Related Questions