I am currently working with an array of objects:
const dummyLinkRows = [
{
id: 'entity:link/1:en',
categories: [
{
name: 'Human Resources'
},
{
name: 'Social'
}
],
name: 'Facebook',
url: 'https://www.facebook.com'
},
{
id: 'entity:link/2:en',
categories: [
{
name: 'Human Resources'
}
],
name: 'Other HR',
url: 'https://www.hr.com'
},
{
id: 'entity:link/3:en',
categories: [
{
name: 'Zen Mode'
}
],
name: 'Zebra',
url: 'https://www.zebra.com'
},
{
id: 'entity:link/4:en',
categories: [
{
name: 'Social'
}
],
name: 'Zebra',
url: 'https://www.instagram.com'
},
];
I am looking to group these links/objects by category for rendering purposes.
The desired structure of the new array should be as follows:
export const NEWDummyLinkRows = [
{
category: { name: 'Social' },
links: [
{
name: 'Facebook',
url: 'https://www.facebook.com'
},
{
name: 'Instagram',
url: 'https://www.instagram.com'
}
]
},
{
category: { name: 'Human Resources' },
links: [
{
name: 'Other HR',
url: 'https://www.hr.com'
},
{
name: 'Zebra HR',
url: 'https://www.zebra.com'
}
]
},
];
In my React render method, I have implemented the following code snippet:
{props.rows &&
props.rows.map((row, index) => {
return (
<div key={index}>
<h4>{get(row, 'categories')[index].name}</h4>
<ul className='link-group--list'>
{row.categories.map((link, index) => {
return (
<li key={index}>
<a href={row.url}>
{link.name}
</a>
</li>
);
})}
</ul>
</div>
);
})}
Although the current implementation renders data, it does not produce the expected output. I am seeking a solution using pure ES6/JavaScript methods.