Merc
Merc

Reputation: 4560

Change string on each loop iteration

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

Upvotes: 3

Views: 1254

Answers (4)

Merc
Merc

Reputation: 4560

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?

Upvotes: 0

Solo
Solo

Reputation: 6957

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));

Upvotes: 1

NikxDa
NikxDa

Reputation: 4187

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;
}

Upvotes: 1

shawon191
shawon191

Reputation: 1955

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, '');
  });
}

Upvotes: 0

Related Questions