-3

i have multiple strings with different keys like name, email and otp.

and i want to embed json values to string dynamincaly with matching keys in json.

like this string.

Dear {user}, Your account for Portal has been created against this Email Address {email}.

and JSON like this.

i want to embed this json into string.

{
   "user": "John Wick",
   "email": "some@email.com"
}

i want result like this after embedding json into string.

Dear John Wick, Your account for Portal has been created against this Email Address some@email.com.

any idea how can i achieve this. thanks in advance.

Muhammad Fazeel
  • 548
  • 6
  • 17

1 Answers1

1

So this example would walk over the keys of the JSON and replace any occurrence that is wrapped in curly braces.

const userDetails = {
   "user": "John Wick",
   "email": "some@email.com"
};

const result = Object.keys(userDetails).reduce((acc, curr) => {
    return acc.replace(`{${curr}}`, userDetails[curr])
}, `Dear {user}, Your account for Portal has been created against this Email Address {email}.`)

console.log(result);
Eray Ismailov
  • 305
  • 3
  • 8