I have the following function that replaces a string occurence with a random number. What I have currently replaces the string with the same random number. I need a function that replaces each instance a unique number. Here's what I have.
Attempt to Clarify:
I want to find all occurrences of '},{
and replace it with },"random":{
where random
is a unique integer for each occurrence.
result = this.replaceAll(result, '},{', `},"${this.getRandomInt(1, 2000)}":{`);
private getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
private replaceAll(str, find, replace) {
return str.replace(new RegExp(this.escapeRegExp(find), 'g'), replace);
}
private escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
Currently the result is something to the effect of
},"1340":{"expense_category_id":"63","amount":3},"1340":{"expense_category_id":"62","amount":3}}}}
1340 should not have duplicated
Edit: The value of result before replace all is:
},{"expense_category_id":"63","amount":3},{"expense_category_id":"62","amount":3}}}}