score:4
Accepted answer
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>
);
});
};
Source: stackoverflow.com
Related Query
- Loop through Array of object with the same data on some object and get it displayed once
- How to map through an Array of object with the same data on some object and get it displayed once and also get other properties
- Loop through json object and display the data with React?
- How to loop through an array and display data within the object of that array, in react and javascript
- How to loop through an object with key and associated array and filter the array based on value presence in another array
- Fetched data from external api,how to loop through the array and display values
- Loop through array and render multiple instances of a child component with that data with React?
- In Javascript, replace forEach that involves filtering the same array of objects each time through loop with a left join
- Loop through array of objects, check for a matching parameter and add the matching object to new array
- Loop through array of object and get values
- react loop through array of strings with delay and set the text value
- Iterate through a for loop with the help of react and read out the data record
- I have spent the last 8 hours trying to pass a simple JSON object with some data from an api through my express backend to one of my react components
- loop through multiple objects inside the first array from JSON data displaying two arrays with objects in React using FUNCTION COMPONENT
- ReactJs - Not able to display all data at once. The loop to get the data runs automatically by re-rendering one by one and stops at the last data set
- How can I store the data I get from a Promise back into an object with the same key it came from?
- How to iterate with Object Dot Keys and Array Dot Map to display data in a component?
- How to combine the previous data with the new data in React and display it at the same time
- I'm trying to delete an object from an array and the response I get is that the data is undefined
- Data object with array of different groups, trying to display ordered by group type using .map() and pushing to new array nothing is rendering
- How to get object value from an array in the local storage and compare it with a new value?
- how to submit the form data into an array using useReducer hook & i have to get the data and to display it?
- Loop through Object which has Array as value and create div for each key with its value as information
- How to loop through fetch array and display results with Next.js
- React.js: loading JSON data with Fetch API and props from object array
- useState hook can only set one object at the time and return the other object of the same array to initial state
- Loop through array of file urls and download each one with React
- How to filter and sort the same array of object state in redux?
- How can i get array of object from this data for state and count in reactjs
- Display data in a React component from an object where the keys are different but the values are the same
More Query from same tag
- NPM: npm install hangs on fetchMetaData -> addTmpTa
- Can't enter text value in input field with ReactJS
- react-router-dom doesn't work
- reactjs: call a function after setState used with prevState
- Can I trigger a onclick event in a function component which updates the state of a class component?
- I am not able to get jwt token from cookies while authorization some pages.?
- react function component Form Component onSubmit Handler Not Working
- Set autocomplete off for Formik field
- React vitest innerHTML toEqual
- configure React (build) routes.js with Django urls.py
- Trying to use props in order to display new comments (React JS)
- Material UI Dialog Component - How to show it?
- Typing the initial state of a custom useForm hook
- 'setLoading' is missing in props validation
- How to implement login authentication using react-redux?
- Reading Excel file in client side JavaScript
- async await in componentDidMount returned strange value
- Deploy both django and react on cloud using nginx
- Best way to group an array with property of object and how to render the result
- how to execute a function which is evoked in a child component as a property in react?
- WithProps vs withHandlers
- React relative href not working as expected
- Take the example of the handling events
- Country-list: Looping through country object to render <options> returns error
- Why is OnChange not working when used in Formik?
- Javascript : How to select elements within one instance of a react class?
- getting material-ui TextField value onsubmit
- getting Network Error when calling axios get request in react js
- How to return an attribute of clicked element in react?
- React hooks not being set properly with JSON API response