Removing quote marks from return values

Hi!

I’m trying to make a .ts file where the API URL is dynamic depending on the current user.

getCurrentUserData() {
    let apiUrl='apiurl.io/user';
    let data: Observable<any> = this.http.get(apiUrl+localStorage.getItem("uid")+'?_format=json');
    data.subscribe(result => {
      this.currentUserData = result;
      console.log(result);
    })
  }

I’m having problems with getting the proper URL.

I’m trying to get " apiurl.io/user/(uid)?_format=json’ ", but what i’m getting instead is " apiurl.io/user/"(uid)"?_format=json ".

I get the %22(uid)%22 and I’m guessing it’s because the stored value of uid is “1” with quotes instead of just the value 1.

Is there a way to format return values in .ts file?

or can anyone suggest an alternative solution? It is greatly appreciated.

Got this from a quick googling, is this what you need?

Thank you @rille.

I’ve been trying the same solution prior to posting, but I’m unable to remove all the double quotation marks. only the first one gets removed.

I seem to be missing something.

I don’t know how to help you here. If I run below code in the browser console I get the correct result:

'apiurl.io/user/"(uid)"?_format=json'.replace(/['"]+/g, '');

gives me:

"apiurl.io/user/(uid)?_format=json"

Below code should work:

getCurrentUserData() {
    let apiUrl='apiurl.io/user';
    let data: Observable<any> = this.http.get(apiUrl+localStorage.getItem("uid").replace(/['"]+/g, '')+'?_format=json');
    data.subscribe(result => {
      this.currentUserData = result;
      console.log(result);
    })
  }

Remember that if you only get the first string then you might miss “+”. As he said in the thread that is important when you want to replace more than one.

1 Like

Thanks again. I’ve tried adding /g for global replacement but .ts file doesn’t seem to recognize the /g part. I may have missed “+” too.

Upon testing other things, I’ve found another solution.

I should’ve just added JSON.parse( localStorage.getItem(“uid”) ). It returned the value without the “” and is now displaying the proper apiurl.io/user/(uid)?_format=json

I like this way to replace all characters in a string

element = element.split('#').join('@');

This replaces all # with @ in my element var.