[Solved] How to substitute a variable in a template string?


After reviewing your updated question, what I think you really want is a function that will accept a template and an object of values to replace with. Here’s a working example.

function PropertyAccess(template, values) {
  for (key in values) {
    template = template.replace('${' + key + '}', values[key])
  }
  
  return template;
}

console.log(PropertyAccess('hello ${val}', {val: 'world'}))

0

solved How to substitute a variable in a template string?