Last active
March 21, 2023 22:51
-
-
Save fxmontigny/4b22a10c5f9887d23a221aaf7acc4697 to your computer and use it in GitHub Desktop.
Recursively find and replace all strings from a object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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