[英]How can I read/write local variables dynamically in JavaScript/TypeScript?
I am pretty new to angular and was wondering how I can access a LOCAL variable dynamically.我对 angular 很陌生,想知道如何动态访问LOCAL变量。 Here is the scenario I am facing:这是我面临的情况:
I am declaring 7 objects within an IF statement:我在IF 语句中声明了 7 个对象:
if (...) {
const day1 = { date: null, total: 0 };
const day2 = { date: null, total: 0 };
const day3 = { date: null, total: 0 };
const day4 = { date: null, total: 0 };
const day5 = { date: null, total: 0 };
const day6 = { date: null, total: 0 };
const day7 = { date: null, total: 0 };
}
Later within that IF statement, I need to assign a value to the "total" key depending on calculations also done within that IF statement.稍后在该 IF 语句中,我需要根据也在该 IF 语句中完成的计算为“total”键分配一个值。
Now, instead of doing something like this later within the IF statement:现在,不要稍后在 IF 语句中执行类似的操作:
day1.date = ... ;
day2.date = ... ;
day3.date = ... ;
day4.date = ... ;
day5.date = ... ;
day6.date = ... ;
day7.date = ... ;
I would like to do something like this to keep my code neat and efficient (obviously, this does not work):我想做这样的事情来保持我的代码整洁和高效(显然,这不起作用):
for (let i = 1; i <= 7; i++) {
['day' + i].date = ... ;
}
It would have worked if the 7 objects were declared globally and then using this['day' + i].date
.如果全局声明 7 个对象然后使用this['day' + i].date
,它会起作用。 But the objects being declared locally in the IF statement, I can't use this syntax.但是在 IF 语句中本地声明的对象,我不能使用这种语法。
My questions are the following:我的问题如下:
Thank you for your support.谢谢您的支持。
Change this:改变这个:
if (...) {
const day1 = { date: null, total: 0 };
const day2 = { date: null, total: 0 };
const day3 = { date: null, total: 0 };
const day4 = { date: null, total: 0 };
const day5 = { date: null, total: 0 };
const day6 = { date: null, total: 0 };
const day7 = { date: null, total: 0 };
// ...
day1.date = ... ;
day2.date = ... ;
day3.date = ... ;
day4.date = ... ;
day5.date = ... ;
day6.date = ... ;
day7.date = ... ;
}
...to this: ...对此:
// Declared in module scope:
type Day = { date: Date | null, total: number };
// Inside your function:
if( ... ) {
const days: Day[] = [
{ date: null, total: 0 },
{ date: null, total: 0 },
{ date: null, total: 0 },
{ date: null, total: 0 },
{ date: null, total: 0 },
{ date: null, total: 0 },
{ date: null, total: 0 }
];
//
for( let i = 0; i < 7; i++ ) {
days[i].date = ...
}
}
You can also initialize days
in a loop too:您也可以在循环中初始化days
:
const days: Day[] = [];
for( let i = 0; i < 7; i++ )
{
days[i] = { date: null, total: 0 };
}
And you can have named references to elements in the array if you want to keep on using day1
, day2
, etc (but remember these are references to the object values in the array rather than being aliases to array indexes):如果您想继续使用day1
、 day2
等,您可以命名对数组中元素的引用(但请记住,这些是对数组中 object值的引用,而不是数组索引的别名):
const day1 = days[0];
const day2 = days[1];
...though try not to let your 1-based day numbering be confused with JavaScript/TypeScript's 0-based array indexing. ...尽管尽量不要让您的基于 1 的日期编号与 JavaScript/TypeScript 基于 0 的数组索引相混淆。
eval
:最坏的解决方案:使用eval
: You can manipulate locals using eval()
, but you shouldn't use eval()
, ever .您可以使用eval()
操作本地人,但永远不应该使用eval()
。 But you can do this:但是你可以这样做:
if (...) {
const day1 = { date: null, total: 0 };
const day2 = { date: null, total: 0 };
const day3 = { date: null, total: 0 };
const day4 = { date: null, total: 0 };
const day5 = { date: null, total: 0 };
const day6 = { date: null, total: 0 };
const day7 = { date: null, total: 0 };
for (let i = 1; i <= 7; i++) {
const expr = 'day' + i + ' = ...;';
console.log( expr );
eval( expr );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.