I have array:
link https://api/v1/3
link https://api/v1/3/user-data
link https://api/v1/3/customer-data
link https://api/v1/3/suppliers-sup
i am filter array :
let res = val.match(/.*\/(.*)/)[1];
second way to filter :
const result = links.reduce((acc, {href: link}) => {
const last = link.split('/').pop();
if (!(last == Number(last))) acc.push(last);
return acc;
}, []);
And right now results is:
[
"user-data",
"customer-data",
"suppliers-sup"
]
first logic way is with "3" item but no important for now.
What i need ? to filter to results be:
[
"User data",
"Customer data",
"Suppliers sup"
]
With first letter big and to remove "-" and apply space
In a concise manner, you can achieve your result as -
console.log(
[
"https://api/v1/3",
"https://api/v1/3/user-data",
"https://api/v1/3/customer-data",
"https://api/v1/3/suppliers-sup"
].reduce((acc, href) => {
let result = href.replace(/.*\/([a-z])?(.*)/,
(_, $1, $2) => $1 ? $1.toUpperCase() + $2 : "")
if (result) return [...acc, result]
return acc
}, [])
)
You can format the data like this:
// Sample Data
const data = [
"user-data",
"customer-data",
"suppliers-sup"
]
const formattedData = data.map(item => {
// Seperate words into an array
let dataItems = item.split("-")
// Capitalize first letter of each word
dataItems = dataItems.map(word => word.charAt(0).toUpperCase() + word.slice(1))
// Join the words back together
return dataItems.join(" ")
})