Reputation: 107
So I am working in typescript where I need to modify an array to some specific pattern.
Here is my array:
["sunday","monday","tuesday"]
This is what I need it to be like:
["day:Sunday","day:Monday","day:Tuesday"]
I have already tried map method like this:
result = arr.map(x => ({day: x}));
But map gives me result some different which is not needed:
[{"day":"sunday"},{"day":"monday"},{"day":"tuesday"}]
Upvotes: 1
Views: 42
Reputation: 1185
const days = ["sunday","monday","tuesday"];
const dayFun = days.map((day) => {
const dayUppercase = day.charAt(0).toUpperCase() + day.slice(1);
return `day:${dayUppercase}`;
});
console.log(dayFun);
Upvotes: 0
Reputation: 2487
Array map is the right method, you just need to return a string, not an object:
result = arr.map(d => `day:${d.toUpperCase()}`)
Upvotes: 1
Reputation: 554
The problem is that you are adding those brackets, here's a solution:
const original = ["sunday","monday","tuesday"]
console.log(original)
const result = original.map(day => `day:${day}`);
console.log(result)
//["day:sunday", "day:monday", "day:tuesday"]
Upvotes: 1
Reputation:
You're trying to prepend the strings and to change the first letter to upper:
const arr = ["sunday","monday","tuesday"];
const result = arr.map(x => 'day:' + x[0].toUpperCase() + x.slice(1));
console.log(result);
Upvotes: 1