So I have this 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'
},
];
Basically what I need to do is to group the links/objects by category so I may render them which the correspondent category title.
I will need a new array to be like this:
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'
}
]
},
];
Here is what I have so far in a react render method:
{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>
);
})}
So far it renders data but not as I need it. The method I need could be using pure ES6/JavaScript.
instagramlink you have name aszebrabut in you desired output it is asInstagrami consider it as typo, please update the question