-4

A function which has 2 arguments:

  1. First argument will have string - 'hello ${val}'
  2. Second argument will be an object - {'val':'world'}

I need to write a solution where the first argument will access the property of the second argument's object and print its value in the first argument's string.

 function PropertyAccess('hello ${val}', {val:'world'}){

       //returns   "Hello World"
  }
AnonymousSB
  • 3,516
  • 10
  • 28
  • That is not even a function for starters. – StackSlave Nov 18 '18 at 09:16
  • 1
    Possible duplicate of [How can I do string interpolation in JavaScript?](https://stackoverflow.com/questions/1408289/how-can-i-do-string-interpolation-in-javascript) – t.m. Nov 18 '18 at 09:16

1 Answers1

0

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'}))
AnonymousSB
  • 3,516
  • 10
  • 28