Skip to content

Instantly share code, notes, and snippets.

@fxmontigny
Last active March 21, 2023 22:51
Show Gist options
  • Save fxmontigny/4b22a10c5f9887d23a221aaf7acc4697 to your computer and use it in GitHub Desktop.
Save fxmontigny/4b22a10c5f9887d23a221aaf7acc4697 to your computer and use it in GitHub Desktop.
Recursively find and replace all strings from a object
const cleanObject = (object) => {
if (object !== null) {
switch (typeof object) {
case 'string' :
const regex = new RegExp('your regex', 'gm');
object = object.replace(regex, 'your new string');
break;
case 'object':
if (object instanceof Array) {
const length = object.length;
for (let i = 0; i < length; i++) {
object[i] = cleanObject(object[i]);
}
} else {
for (let i in object) {
object[i] = cleanObject(object[i]);
}
}
break;
}
}
return object;
};
// replace domain name to main domain name
result = cleanObject(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment