1

我正在尝试将两个数组组合成一个一起工作(不是concat()),我正在寻找有关最佳实践和建议的建议。目标是创建一个脚本(仅 JavaScript,不是 jQuery),从存储在第一个数组中的证词列表中随机写入员工证词,然后通过查看第二个数组动态写入他们在公司工作的年限数组,找到他们的雇用日期(仅限年份),然后从当前年份中减去该日期。

这是我的第一个数组:

<script>
  var r_text = new Array ();
  r_text[0] = "John Smith - Customer service - Year 5! 'The thing I love the most about company x is the people, the way the company rallies around an employee in need is truly amazing and something I have never seen before.  Company x truly has a family feel and the approachability of both management and supervisors makes for a great place to work'";
  r_text[1] = "Jane Smith - Back office - Year 9! 'The pay is above average and we have benefits, like a great health plan, paid sick days, and paid holidays.  The people are friendly and we help each other.  Supervisors are there to help you, one on one, so you can do your job efficiently.' ";
  r_text[2] = "Jane Doe - Trainer - Year 7! 'I feel lucky because I can actually say I love coming to work everyday. The atmosphere here is professional - yet personal at the same time. We are a business, yes, but we are also a family.  Our dedicated, approachable leadership team is always right behind you to help you achieve your goals.  We  come together in times of need whether it be for a local cause or to help an employee in need and it really makes me proud to say I work here and have for so many years.'";
    var i = Math.floor(3*Math.random())
    document.write(r_text[i]);
</script>

如您所见,名称和年份已经在第一个脚本中。这就是我想要摆脱的。我想让这个脚本是自给自足的,并且不需要任何帮助,所以我希望这些年能在没有我或其他任何人进入并修改字符串的情况下更新。

好的,现在第二个数组是我遇到第一个问题的地方

我有一个简单的函数来获取今年并从存储在变量中的设定年份中减去它以获得任期:

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Get tenure</button>

<script>
function myFunction()
{
var d = new Date();
var h = 2003 //hire date
var x = document.getElementById("demo");
document.write(d.getFullYear()-h + " years");
}
</script>

</body>
</html>

好的,所以我知道我需要重新编写这个简单的函数来将每个人的任期存储在一个数组中。我试过了:

<script>
  var hireYear = new Array ();
  hireYear[0] = 2008; //John Smith
  hireYear[1] = 2004; //Jane Smith
  hireYear[2] = 2006; //Jane Doe
   var d = new Date();
   var h = hireYear[i];
     for (var i=0;i<hireYear.length;i++)
     { 
     document.write(d.getFullYear() - hireYear[i] + " Years. <br />");
     }                      
</script>

打印出来:

5 Years. 
9 Years. 
7 Years. 

但我的困境是我现在如何让这两个阵列互相交谈?我认为由于第一个数组使用的是 Math.random,第二个数组将是子数组,所以我应该更改r_text[0] = "John Smith - Customer service - Year 5!r_text[0] = "John Smith - Customer service - hireYear[0]但除此之外我迷路了。我尝试搜索两个数组一起工作的类似示例,但我能找到的只是concat()示例,这似乎不适用于我的需求。

注意:我不能在 jQuery 中做到这一点,它必须在 JavaScript 中完成。我知道我不应该使用document.write,所以如果有人有更好的建议,欢迎他们。

非常感谢您提供的任何提示、建议或批评!

4

1 回答 1

1

您可以将年份与数组元素本身联系起来,而不是保留 2 个单独的数组。您可以将一个对象存储在一个数组元素中,其中一个属性text持有证明,但YYYY占位符用于实际年份,第二个属性yearHired持有该人被雇用的年份:

var r_text = [];
r_text[0] = {text: "John Smith - Customer service - Year YYYY! 'The thing I love the most about company x is the people, the way the company rallies around an employee in need is truly amazing and something I have never seen before.  Company x truly has a family feel and the approachability of both management and supervisors makes for a great place to work'",
             yearHired: 2008};

r_text[1] = {text: "Jane Smith - Back office - Year YYYY! 'The pay is above average and we have benefits, like a great health plan, paid sick days, and paid holidays.  The people are friendly and we help each other.  Supervisors are there to help you, one on one, so you can do your job efficiently.' ",
             yearHired: 2004}

r_text[2] = {text: "Jane Doe - Trainer - Year YYYY! 'I feel lucky because I can actually say I love coming to work everyday. The atmosphere here is professional - yet personal at the same time. We are a business, yes, but we are also a family.  Our dedicated, approachable leadership team is always right behind you to help you achieve your goals.  We  come together in times of need whether it be for a local cause or to help an employee in need and it really makes me proud to say I work here and have for so many years.'",
             yearHired: 2006}

这样,您只需从数组中选择一个元素,计算服务年份并将 YYYY 占位符替换为结果:

var i = Math.floor(r_text.length * Math.random());
var d = new Date();
var s = r_text[i].text.replace('YYYY', d.getFullYear() - r_text[i].yearHired);
document.write(s);

这是一个演示:http: //jsfiddle.net/hrz4g/2/

你是对的,document.write不是显示数据的最佳选择。更好的方法是使用占位符,例如 DIV,并使用innerHTML属性在其中显示数据。

这是一个更新的演示,展示了这种方法:http: //jsfiddle.net/hrz4g/3/

于 2013-10-07T18:19:00.653 回答