0

I have a flexible amount of titles, aka title1, title2, title3 etc. How can i create a dynamic variable that gives me the value of that field?

function getTitle(i){
    var myScope={}
    myScope["title"+i]="title"+i // should i use this in any way, and how?

    var output = props.attributes.title1 // This works, but it is static. Instead the 1, add the value of i here like:
    var output = props.attributes.title+i
    return output
}

I would like to concatenate the value of i to the word 'title'. So it becomes title1 or title2 etc.

Boberwt
  • 23
  • 3

2 Answers2

1

You can make it dynamic like this

var output = props.attributes[`title${i}`];
Abito Prakash
  • 4,368
  • 2
  • 13
  • 26
0

If I understood your question right you can use template literals.

function getTitle(i) {
  let title = 'myTitle'

  let output = `${title} ${i}`
  console.log(output)
  return output
}

getTitle('concated')
seriously
  • 1,202
  • 5
  • 23