let string = 'My name is [~FIRSTNAME] [~LASTNAME]';
let nameArray = ['Peter', 'Parker'];
let patternToBeReplaced = ['[~FIRSTNAME]', '[~LASTNAME]']
I want to replace the string with the elements of namearray so that the string becomes 'My name is Peter Parker'
Below is my approach to this problem -
patternToBeReplaced.forEach(function (match, index) {
var output = string.replace(match, nameArray[index]);
});
But this is not working as expected.
string = string.replace(...reduceinstead offorEach