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

2
  • Wo don't you want to reassign a function parameter? It is perfectly valid for this case to do so. Commented Dec 13, 2018 at 12:29
  • Possible duplicate of stackoverflow.com/questions/53758183/… Commented Dec 13, 2018 at 12:30

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

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...
Sure, just give it a shot if you can :) Let me know if anything still doesn't fit what you're looking for
Wow, I must have made some other error. This does work indeed. I could have sworn that I tried this... Anyway. Thank you.
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));

1 Comment

Thank you. This is the same solution which NikxDa postet. I think NikxDa was faster though :)..
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, '');
  });
}

1 Comment

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...
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.