0

In Javascript, I am trying to take an array and make it a string with curly braces with each word.

Input: ["apple", "brand", "title"]

Output should be: '{apple} {brand} {title}'

I tried with JSON.stingify or toString() however couldn't get the expected output. I have no knowledge about regex.

Later on I need to take '{apple} {brand} {title}' and convert back to array. Is there any way to accomplish this easily?

1 Answer 1

2

Is this what you're looking for?

console.log(
  ["apple", "brand", "title"].map(el => `{${el}}`).join(' ')
);

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

3 Comments

Yes! Do you also know how can I do this backward? Take the string and convert to array?
'{apple} {brand} {title}'.replace(/[{}]/g, '').split(' ')
You are great! I will accept the answer whenever I can

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.