I have this string:
saidogAngry Kappa saidogAngry saidogAngry ResidentSleeper ResidentSleeper
I need to replace parts of this string with rep
based on an array of this kind:
const eleArray = [{
"start": "0",
"end": "10",
"rep": "63f5ce1593c142a09d714b34e1938990"
}, {
"start": "12",
"end": "16",
"rep": "25"
}, {
"start": "18",
"end": "28",
"rep": "63f5ce1593c142a09d714b34e1938990"
}, {
"start": "30",
"end": "40",
"rep": "63f5ce1593c142a09d714b34e1938990"
}, {
"start": "42",
"end": "56",
"rep": "245"
}, {
"start": "58",
"end": "72",
"rep": "245"
}]
The problem is that after the first replace
, all other indexes are altered.
My initial idea was to split
it in single sections and then remove/replace them
const msgArray = [];
let lastIndex = 0;
eleArray.forEach(ele => {
msgArray.push(message.slice(Number(lastIndex), Number(ele.start)));
// msgArray.push(message.slice(Number(ele.start), Number(ele.end+1)));
msgArray.push(ele.rep);
lastIndex = Number(ele.end)+1;
});
console.log(msgArray.join('');
Is there a simpler or a more optimized way? I need to have the best best performance out of it because I’m creating a text stream with a big quantity of messages.
2