1

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"}]

4 Answers 4

1

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);

Sign up to request clarification or add additional context in comments.

Comments

1

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"]

1 Comment

Your answer doesn't produce the expected result ["day:Sunday","day:Monday","day:Tuesday"] and you should use let/const instead of var.
1

Array map is the right method, you just need to return a string, not an object:

result = arr.map(d => `day:${d.toUpperCase()}`)

1 Comment

This answer changes all characters, not only the first.
0
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.