I am basically taking a URL like http://localhost:3000/north-america/america and then after the third slash putting each entry into an array.
I have an array of strings like this ["america", "north-america"].
I want to capitalise every first letter of each entry and each word in each entry and then join the strings back together to give me this result ["America", "North-America].
I also want to remove the dashes from any entry and replace them with a space giving me the final result of ["America", "North America].
So far I have managed to get this ["America", "North america"] but I am struggling to capitalise the second word.
const urls = "http://localhost:3000/north-america/america"
useEffect(() => {
if (withAvailabilityChecker === true && urlS !== undefined) {
const url = urlS;
let parts: string[] = [];
parts = url.split('/');
parts.splice(0, 3);
parts.reverse();
parts.splice(0, 1);
const newParts: string[] = [];
parts.forEach(el => {
const removeDashes = el.replaceAll('-', ' ');
const capitaliseEntry = removeDashes.charAt(0).toUpperCase() + removeDashes.slice(1);
newParts.push(capitaliseEntry);
});
if (newParts.length > 2) {
newParts.length = 2;
}
const result = newParts.join(',' + ' ');
setInputVal(result);
}
}, [urlS, locationName]);