1

I have a replace function that works like this:

let componentJSON = [
  { "template": "<div class='${layout} ${border}'></div>" },
  {
    "layout": "grid",
    "border": "primary"
  }
];

const template = componentJSON[0].template
const classes = componentJSON[1]

let html = template.replace(/\$\{(.*?)\}/g, (match, key) => classes[key]);

console.log(html);

There's one part of this that I don't understand, and that's the classes[key] at the end of the arrow function - is this something specific to replace? I think I've seen it in map methods too.

Would be interested to see if there's a name for this or another clearer way of writing it?

daggett
  • 239
  • 2
  • 4
  • 15

0 Answers0