2013-10-07 49 views
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,所以如果有人有更好的建議,他們是受歡迎的。

非常感謝您提供的任何提示,建議或批評!

+1

而不是'document.write',看看'innerHTML' - '的document.getElementById( '示範')innerHTML'。你可以設置它並從中檢索。這並不理想,但它是最易讀的替代方案。 – Izkata

回答

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/

+0

天才! : ) 非常感謝! – abracassabra

+0

沒問題:)很高興它爲你工作 –