I have two arrays authors and posts. I want to return the posts arrays with the authors names not their emails, as seen below.
const authors = [
{name: 'Thompson Smith', email: '[email protected]'},
{name: 'John Doe', email: '[email protected]'},
{name: 'Jane Coker', email: '[email protected]'},
{name: 'Mirabel Ekong', email: '[email protected]'},
{name: 'Samuel Doe', email: '[email protected]'},
{name: 'Moses Philips', email: '[email protected]'},
{name: 'Marcus Bowa', email: '[email protected]'},
{name: 'Peter Touch', email: '[email protected]'},
{name: 'Benson Bruce', email: '[email protected]'},
]
const posts = [
{ title: 'title one', authors: ['[email protected]', '[email protected]', '[email protected]'] },
{ title: 'title two', authors: ['[email protected]', '[email protected]', '[email protected]'] },
{ title: 'title three', authors: ['[email protected]', '[email protected]', '[email protected]'] },
{ title: 'title four', authors: ['[email protected]', '[email protected]', '[email protected]'] },
]
I want to return the posts with the actual name of the authors like this
<div>
<h2>{post.title}</h2>
<p>
<span>{post.author.name}</span>
<span>{post.author.name}</span>
<span>{post.author.name}</span>
</p>
</div>
Please how can I achieve this in react/javascript?
EDIT: I forgot to add some really important parts of the question.
In the
postsarray, there are some that have the actual names of the authors (not emails) and these names does not occur in the authors array, for example;{ title: 'title one', authors: ['Michael Johnson', '[email protected]', '[email protected]'] }
In this case, I also want to retrieve the name Michael Johnson and retrieve the names of the rest of the authors from the authors array.
- In the
authorsarray, there are extra props that I want to retrieve, such as theuserIdandavatar. In essence the code looks like this;
const authors = [
{name: 'Thompson Smith', email: '[email protected]', userId: '001', avatar: '/avatar/1'},
{name: 'John Doe', email: '[email protected]', userId: '002', avatar: '/avatar/2'},
{name: 'Jane Coker', email: '[email protected]', userId: '003', avatar: '/avatar/3'},
{name: 'Mirabel Ekong', email: '[email protected]', userId: '004', avatar: '/avatar/4'},
{name: 'Samuel Doe', email: '[email protected]', userId: '005', avatar: '/avatar/5'},
{name: 'Moses Philips', email: '[email protected]', userId: '006', avatar: '/avatar/6'},
{name: 'Marcus Bowa', email: '[email protected]', userId: '007', avatar: '/avatar/7'},
{name: 'Peter Touch', email: '[email protected]', userId: '008', avatar: '/avatar/8'},
{name: 'Benson Bruce', email: '[email protected]', userId: '009', avatar: '/avatar'}
]
const posts = [
{ title: 'title one', authors: ['[email protected]', '[email protected]', '[email protected]'] },
{ title: 'title two', authors: ['[email protected]', '[email protected]', '[email protected]'] },
{ title: 'title three', authors: ['[email protected]', '[email protected]', '[email protected]'] },
{ title: 'title four', authors: ['[email protected]', '[email protected]', '[email protected]'] },
{ title: 'title five', authors: ['michael Johnson', '[email protected]', '[email protected]'] },
{ title: 'title six', authors: ['michael Johnson', 'Jane Joshua', '[email protected]'] },
]
EXPECTED OUTPUT
<div>
<h2>{post.title}</h2>
<ul>
<li><Link to={userId}><Avatar src={avatar}/>{post.author.name}</Link></li>
<li><Link to={userId}><Avatar src={avatar}/>{post.author.name}</Link></li>
<li><Link to={userId}><Avatar src={avatar}/>{post.author.name}</Link></li>
//If the author does not exist in the authors array, should return
<li><placeHolderAvatar />{post.author.name}</li>
</ul>
</div>
Please how do I retrieve these extra props?