first time using stackoverflow. :)
I am a beginner in JS trying to add values to a list of variables that may change based on the number of passengers variable. (ie. if numRiders = 4 I need to assign values to passenger1Name, passenger2Name, passenger3Name, passenger4Name)
I am trying to use eval inside a loop to do it:
for(i = 0; i<=numRiders; i++) {
j = i+1
var l ='var '
var k = 'passenger'
let nameJ = ride[i].passengerDetails.first + ' ' + ride[i].passengerDetails.last;
console.log (nameJ)
eval(l+k+j+ 'Name' + '= ' + nameJ + ';')
console.log(passenger1Name)
I am getting this output right after the nameJ console.log
VM321:1 Uncaught SyntaxError: Unexpected identifier
at pageLoad
Anyone know how can I solve this or approach this differently? Unfortunately, I can't change the variables names (e.g. passengerXName) to one that would make it easier to assign dynamic variables.
Thank you
question from:
https://stackoverflow.com/questions/65945143/using-eval-uncaught-syntaxerror-unexpected-identifier-at-pageload 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…