Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update the string value in the object based on the parameter – Typescript

I have exported an object in a separate ts file and I would like that to be parameterized

export const customer = {
  comment: 'Wow, this is great because {{username}} is doing what we needed' 
};

Then in another ts file, I get this constant but I want to pass the value of username

import * as customerConstants from 'constants';
export class CustomerComponent implements OnInit{
 username ='learn' 
 customerData = customerConstants.customer ;

  ngOnInit(): void {
    this.customerData.comment = customerData + username;
  }

I want to put learn in place of username in the customer const. Is it possible?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since customer.comment is string you could use replace() to replace '{{username}}' with username variable value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading