1

I have an array of products like this

products: [
  {
    id: 1, 
    drinkName: "Chivita", 
    category: "Juice", 
    description: "The best drink ever"
  },
  {
    id: 1, 
    drinkName: "5 Alive", 
    category: "Juice", 
    description: "The best drink ever"
  },
  {
    id: 3, 
    drinkName: "Cocacola", 
    category: "Others", 
    description: "The best drink ever"
  }
];

What I want is to loop through the array and get the category displayed only once, I know each drink can have the same category, but I want it displayed only once, and also get the drinkName display under each category.

Example:

products.map((product) => {
     <AccordionItem key={productsSnapshot?.id}>
         <h2>
          <AccordionButton fontSize={"sm"} fontWeight={"medium"}>
             <Box flex="1" textAlign="left">
               {product?.category}
             </Box>
            <AccordionIcon />
          </AccordionButton>
        </h2>
    </AccordionItem>

});

For example, I have a let's say two drinkName with the category "Juice", if I try to map through the products and want to get the product?.category, I will have Juice printed twice, so I want it to get printed once, if it exists twice print only once -- I hope you understand

Is that possible?

5
  • What do you mean by "category displayed only once"? Commented Mar 11, 2022 at 4:45
  • 2
    rearrange your data ...categories:{Juice:[{id:1,drinkName:"Chivita",category:"Juice",description:"The best drink ever"},{id:1,drinkName:"5 Alive",category:"Juice",description:"The best drink ever"}],Others:[{id:3,drinkName:"Cocacola",category:"Others",description:"The best drink ever"}]} - easily done with reduce or similar Commented Mar 11, 2022 at 4:49
  • Please check the question again slideshowp2 Commented Mar 11, 2022 at 4:52
  • @Bravo, the data is coming from firestore, and over 180 product has been there. Commented Mar 11, 2022 at 4:53
  • 1
    I didn't say you would do it by hand! that's what array functions like reduce are for ... products.reduce((a, o) => ({ ...a, [o.category]:[...(a[o.category] || []), o]}), {}) Commented Mar 11, 2022 at 4:58

1 Answer 1

4

Like @Bravo says:

const products = [
  {
    id: 1,
    drinkName: "Chivita",
    category: "Juice",
    description: "The best drink ever",
  },
  {
    id: 1,
    drinkName: "5 Alive",
    category: "Juice",
    description: "The best drink ever",
  },
  {
    id: 3,
    drinkName: "Cocacola",
    category: "Others",
    description: "The best drink ever",
  },
];

const MyComponent = () => {
  const items = products.reduce((prev, current) => {
    if (!current.category in prev) {
      prev[current.category] = [];
    }
    prev[current.category].push(current.drinkName);
    return prev;
  }, {});

  Object.keys(items).map((key) => {
    return (
      <AccordionItem key={key}>
        <h2>
          <AccordionButton fontSize={"sm"} fontWeight={"medium"}>
            <Box flex="1" textAlign="left">
              {key}
            </Box>
            <Box flex="1" textAlign="left">
              {items[key].map((drinkName) => {
                return <p key={drinkName}>{drinkName}</p>;
              })}
            </Box>
            <AccordionIcon />
          </AccordionButton>
        </h2>
      </AccordionItem>
    );
  });
};
Sign up to request clarification or add additional context in comments.

7 Comments

Please I'm getting error in typescript, please refactor to typescript's way
The left-hand side of an 'in' expression must be a private identifier or of type 'any', 'string', 'number', or 'symbol'.
@AbdulAzeezOlanrewaju there's no mention of typescript in your question. If there's a new issue, you should ask a new question if you can't figure it out on your own.
you should tag your question with typescript then
I have tagged it
|

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.