1

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.

2
  • please add some data and wanted result. Commented Jun 28, 2021 at 18:34
  • .map is intended for returning transformed data, but you use it for side effects on state.user.menu. This could be confusing to other developers. Array.protoype.map -> The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. Commented Jun 28, 2021 at 19:15

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Alright, this looks promising, but I kinda lied, the array that I am using to filter isn't just the ids it is another object with the the id an some gibberish data. So in the place you have ids.includes(id) I should replace ids with state.user.items or state.user.items.id? Since when I dont use id it just throws an error and when I dont use I get the correct category object but with every item inside it.
@GabrielValente I don't understand, why can't you provide the real example input?
Yeah, my bad, I thought it would confuse more since in my head I would just need to do .id to get it. in any case I have updated the question.
You can get the array param by generating one from your updated list as follows: arr.map(({ id }) => id). Would that solve the issue?
Oh my god, it's working, the return data is a bit of a mess but it's way better than what I could do. Thank you for your help and im sorry for the way I complicated everything.
|
0

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

1 Comment

It's a bit more than that, since I have to go through each item in the first array searching for an id in the array that is inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.