如何用另一个变量值定义一个变量

How to define a variable with another variable value

我有一个变量列表,其中日期作为变量名称,例如 a290322、a300322、a310322 等。

var a270322 = "https://google.com" ;
var a280322 = "https://youtube.com" ;
var a290322 = "https://facebook.com" ;
var a300322 = "https://twitter.com" ;
var a310322 = "https://instagram.com" ;
var a010422 = "https://sgquizdaily.com" ;
var a020422 = "https://maps.google.com" ;
var a030422 = "https://gmail.com" ;
var a040422 = "https://docs.google.com" ;

现在我想声明一个名为 Today

的新变量

Today 变量的值应与变量列表中的变量匹配。

我可以每天手动修改如下行。

var today = a300322 ;

但是我不知道怎么通过脚本获取。

你能帮忙将今天的日期变量值分配给今天吗?

您可以将变量存储到一个对象中,这样您就可以像这样基于键存储和调用值

var a270322 = "https://google.com" ;
var a280322 = "https://youtube.com" ;
var a290322 = "https://facebook.com" ;
var a300322 = "https://twitter.com" ;
var a310322 = "https://instagram.com" ;
var a010422 = "https://sgquizdaily.com" ;
var a020422 = "https://maps.google.com" ;
var a030422 = "https://gmail.com" ;
var a040422 = "https://docs.google.com" ;

const variables = {
  a270322,
  a280322,
  a290322,
  a300322,
  a310322,
  a010422,
  a020422,
  a030422,
  a040422,
};

var today = 'a300322';

console.log(variables[today]); // https://twitter.com

通过Date()获取当前日期,填充到2位,切去最后两位,加上'a'和eval前缀。

var a270322 = "https://google.com" ;
var a280322 = "https://youtube.com" ;
var a290322 = "https://facebook.com" ;
var a300322 = "https://twitter.com" ;
var a310322 = "https://instagram.com" ;
var a010422 = "https://sgquizdaily.com" ;
var a020422 = "https://maps.google.com" ;
var a030422 = "https://gmail.com" ;

const now = new Date()
var day = ('0' + now.getDate()).slice(-2)
var month = ('0' +(now.getMonth() + 1).toString()).slice(-2)
var year = (now.getFullYear().toString()).slice(-2)
var Today = 'a'+ day + month + year
console.log(Today)

TodaysUrl = eval(Today)
console.log(TodaysUrl)