I am trying to create dynamic variable, for example rather saying
let f0, f1 = '';
and then using these variable in forEach
{Object.keys(shop).forEach((element, key) => {
if (element == dName[0]) {
f0 = Object.values(shop)[key];
}
if (element == dName[1]) {
f1 = Object.values(shop)[key];
}
})}
trying below,
let k = 'f';
let i = 0;
for(i = 1; i < 2; i++) {
eval('let ' + k + i + '= \'\' ;');
}
console.log("f1=" + f1);
but console printing
f1=undefined
what wrong I am doing, thanks in advance
i don't know if it is possible with eval but you could use an object to store your variables like this
let k = 'f'
let vars = {}
for(i = 1; i < 5; i++) {
vars[k+i] = ''
}
console.log(vars)
console.log("f1=" + vars.f1);