const person = {
name: 'John',
middle: 'S',
last: 'Smith'
}
const format = "{ name } { middle } { last}";
The curly braces acts like a placeholder for dynamic values. i:e {name} will get replaced with John, {middle} with S.
The issue I'm facing is with preserving the spaces in between the format. i:e if there exist two spaces in the {name} {middle}, then the output should be John S.
I'm replacing the placeholder with actual values and joining it with the single space, but it would be static always.
const person = {
name: 'John',
middle: 'S',
last: 'Smith'
}
const format = "{ name } { middle } { last}";
function formatStr(obj, format) {
return format.match(/\b\w+\b/g).map((s) => person[s]).join(' ');
}
console.log(formatStr(person, format));
Help would be really appreciated :)