I am trying to replace the params of a url with values of an object if the param is the same as a value within the object.
I've got it so that it replaces the values but its just creating an array with 2 items and im looking to basically "merge" them together to create a URL.
Any help is much appreciated!
where urlData consists of something like this which is being passed in:
[
{
param: ':userId',
value: userId || '',
},
{ param: ':messageId', value: messageId || '' }
]
And my Menu looks like this:
{menu?.map((menuItem, index) => {
const { title, path } = menuItem
const finalPath = urlData?.map(
(url, index) =>
path.includes(url?.param)
? path.replace(
url?.param,
url?.value
)
: path
)
console.log('final', finalPath)
return (
<Link
to={finalPath}
key={index}
className={
pathname === finalPath ||
finalPath.includes(pathname)
? 'active'
: ''
}
>
{title}
</Link>
)
})}
My current response:
[
"/app/messages/be493d77/:messageId",
"/app/messages/:userId/2cd4df7d"
]
What im looking for:
"/app/messages/be493d77/2cd4df7d"
urlDataarray the result will be an array of equal length. What is the starting value you are trying to replace path segments of?/app/messages/:userId/:messageIdand id like to replace the params with the valuesurlDataarray into the"/app/messages/:userId/:messageId"path string?paththen replace it with the correspondingvaluein that object.