所以下面我有一个变量newVal。在该变量内部是诸如profileNum1之类的对象,在该userEmail内部,我可以通过
访问 let profile = newVal.profileNum1.userEmail;
将返回诸如“ users@email.com”之类的字符串
let newVal = JSON.parse(value);
for (let z = 0; z < 100; z++) {
console.log('LOOP HAS BEGAN RUNNING' + z);
let profile = newVal.profileNum1.userEmail;
console.log('LOOK FOR MEEEEEEEEEEEEEE' + profile);
现在我基本上需要将变量z添加到profileNum中,以代替1。
我尝试通过let profile = newVal.profileNum + z.userEmail;
哪一个不起作用,它只会返回NaN,所以我假设我无法那样做,因为在循环期间使用z = 1时,它在循环中达到z = 1时应该返回结果,但仍然返回NaN 。我很困惑如何将变量Z添加到变量中,并且仍然使用它来选择对象profileNum1,profileNum2等。任何帮助将不胜感激=]
答案 0 :(得分:0)
使用方括号符号:
let profile = newVal["profileNum" + z].userEmail;
这将解析profileNum0
,profileNum1
,profileNum2
等并进行访问-基本上是这样:
let profile = newVal.profileNum0.userEmail; // When z is 0
let profile = newVal.profileNum1.userEmail; // When z is 1
let profile = newVal.profileNum2.userEmail; // When z is 2