Skip to content

Instantly share code, notes, and snippets.

@pawiromitchel
Created March 11, 2021 15:06
Show Gist options
  • Save pawiromitchel/61d9296683d2d913667ba54b7883f4d4 to your computer and use it in GitHub Desktop.
Save pawiromitchel/61d9296683d2d913667ba54b7883f4d4 to your computer and use it in GitHub Desktop.
Dynamic global search replace on records
var fields = [
"name",
"lastname"
]
var rules = {
"!": "[uitroepteken]",
"#": "[hashtag]",
}
var records = [
{
id: 1,
name: "mi!chel"
},
{
id: 2,
name: "!k ben g#k"
}
];
// loop door fields
fields.forEach(value => {
records.forEach(record => {
// check if the field exists
if(record[value]) {
// replace the characters aan de hand van de regels
Object.entries(rules).forEach((values, index) => {
let reg = new RegExp(values[0], "g");
record[value] = record[value].replace(reg, values[1]);
});
}
});
});
console.log(records);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment