Created
March 11, 2021 15:06
-
-
Save pawiromitchel/61d9296683d2d913667ba54b7883f4d4 to your computer and use it in GitHub Desktop.
Dynamic global search replace on records
This file contains hidden or 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
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