-1

I need to create some substrings using the template string. I try to use this code but I have some problem about the auto filling of the template.

        Object.keys(optionsParams).forEach(function (key) {
            let template = '&${key}=${value}';
            let value = optionsParams[key];
            url += template;
        });

key and value variables are not found. What's the problem?

Safari
  • 11,437
  • 24
  • 91
  • 191
  • You aren't using a template literal, and you tried to use `value` before you defined it. Also consider using `Object.entries` instead, to get both the key and value at once – CertainPerformance Jun 15 '19 at 12:40
  • check out `var search = Object.entries(optionsParams).map(([key,value]) => \`${encodeUriComponent(key)}=${encodeUriComponent(value)}\`).join("&")` – Thomas Jun 15 '19 at 12:46

1 Answers1

0

To use template strings you need to use backticks ` instead of single or double quotes. Try the following code instead

Object.keys(optionsParams).forEach(function (key) {
    let value = optionsParams[key];
    let template = `&${key}=${value}`
    url += template;
});
Thomas
  • 11,958
  • 1
  • 14
  • 23