3

I have the following problem: I have a string and I need to replace for different regex parts.

therefore I want to loop over it:

In my case its a URL and I want to empty for specific parameters:

cleanURL(url, params) {
  params.forEach((param) => {
  url = this.updateUrlParameter(url, param, '');
});

Whereas I have:

updateUrlParameter(url, param, value) {
  const regex = new RegExp(`(${param}=)[^&]+`);
  const newURL = url.replace(regex, `$1${value}`);
  return newURL;
}

This is the only way I got it to work, but now I am reassigning a function parameter. this is not what I want.

My problem is, that

cleanURL(url, params) {
  params.forEach((param) => {
    this.updateUrlParameter(url, param, '');
  });
}

Would always for each iteration pass the SAME url into the loop. And I end up with just the last replacement.

I somehow would have to pass a changed string into the next loop iteration.

How could I achieve this? Maybe into this direction? With somehow nesting and calling the function again?

while(outerI--){
  (function(i){
    i+=1;//doesn't affect outerI and you wanted 1-length so we add one.

    //crap inside your forEach loop but without the i++
  })(outerI)
}

which I found here: .forEach loop: use variable

Woudl be very glad for a hint here. thank you

Cheers

Merc
  • 4,241
  • 8
  • 52
  • 81
  • Wo don't you want to reassign a function parameter? It is perfectly valid for this case to do so. – NikxDa Dec 13 '18 at 12:29
  • Possible duplicate of https://stackoverflow.com/questions/53758183/change-query-parameters-of-url-using-dynamic-parameter-and-value-in-jquery/53758609#53758609 – Code Maniac Dec 13 '18 at 12:30

4 Answers4

1

Reassigning the function parameter would be perfectly fine in this use-case.

If you really don't want to reassign a function parameter for whatever reason, you can introduce a new variable to store the updated URL for you.

cleanURL(url, params) {
    let newUrl = url;
    params.forEach((param) => {
        newUrl = this.updateUrlParameter(newUrl, param, '');
    });
    return newUrl;
}
NikxDa
  • 4,137
  • 1
  • 26
  • 48
  • I think I tried this, but must have messed up somehow. I will try this soon (can't right now). Looks simpler than my recursive function... – Merc Dec 13 '18 at 12:46
  • Sure, just give it a shot if you can :) Let me know if anything still doesn't fit what you're looking for – NikxDa Dec 13 '18 at 13:06
  • 1
    Wow, I must have made some other error. This does work indeed. I could have sworn that I tried this... Anyway. Thank you. – Merc Dec 13 '18 at 14:55
1

If you don't want to mutate parameter, use a temporary variable to hold the result.

function updateUrlParameter(url, param, value) {
  const newURL = url + param;
  return newURL;
}

function cleanURL(url, params) {
  let temp = url;
  params.forEach((param) => {
    temp = updateUrlParameter(temp, param, '');
  });
  return temp;
}

const params = [1, 2, 3, 4, 5];
const url = 'string';
console.log(cleanURL(url, params));
Solo
  • 6,687
  • 7
  • 35
  • 67
  • Thank you. This is the same solution which NikxDa postet. I think NikxDa was faster though :).. – Merc Dec 13 '18 at 14:56
0

The String.prototype.replace does not modify the original string, rather it creates a new string.

If you update your clearUrl method to reassign url in each iteration you should get replacements for all the parameters

cleanURL(url, params) {
  params.forEach((param) => {
    url = this.updateUrlParameter(url, param, '');
  });
}
shawon191
  • 1,945
  • 13
  • 28
  • 1
    He mentioned clearly that he had this working, but did not want to reassign a function parameter. Therefore, I think this is not what OP is looking for... – NikxDa Dec 13 '18 at 12:32
0

I also found a way in the meantime:

cleanURL(url, params) {
  const paramsLength = params.length;
  const cleanURL = url;

  const recursive = (i, originalURL) => {
    this.log('i: ', i);
    this.log('originalURL: ', originalURL);
    if (i > 0) {
      const changedURL = this.updateUrlParameter(originalURL, params[i - 1], '');
      return recursive(i - 1, changedURL);
    }
    return originalURL;
  };
  // this.log('url: ', url);
  return recursive(paramsLength, cleanURL);
}

updateUrlParameter(url, param, value) {
  const regex = new RegExp(`(${param}=)[^&]+`);
  // this.log('regex: ', regex);
  const newURL = url.replace(regex, `$1${value}`);
  // this.log('newURL: ', newURL);
  return newURL;
}

Thoughts on that?

Merc
  • 4,241
  • 8
  • 52
  • 81