2 つのアレイを組み合わせて ( ではなく) 1 つのアレイにしようとしていconcat()
ます。ベスト プラクティスと推奨事項に関するアドバイスを探しています。目標は、最初の配列に保存されている証言のリストからランダムな従業員の証言を書き込むスクリプト (jQuery ではなく JavaScript のみ) を作成し、2 番目の配列を見て従業員の勤続年数を動的に書き込むことです。配列、雇用日 (年のみ) を見つけ、現在の年からそれを減算します。
これが私の最初の配列です:
<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>
ご覧のとおり、名前と年は最初のスクリプトに既に含まれています。これは私が離れたいものです。私はこのスクリプトを自給自足で行い、今後の支援を必要としないようにしたいので、私や他の誰かがそこに行って文字列を変更することなく年を更新したいと考えています.
さて、2 番目の配列は、最初の問題を抱えている場所です
私は、今年を取得し、変数に格納された設定年から減算して在職期間を取得するこの単純な関数を持っています。
<!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.
しかし、私のジレンマは、これら 2 つのアレイを相互に通信させるにはどうすればよいかということです。最初の配列は Math.random を使用しているため、2 番目の配列は子配列になるので、変更r_text[0] = "John Smith - Customer service - Year 5!
する必要r_text[0] = "John Smith - Customer service - hireYear[0]
がありますが、それ以上は失われます。2 つの配列が連携して動作する同様の例を検索しようとしましたが、見つけることができたのはconcat()
例だけであり、それは私のニーズには当てはまらないようでした。
注:これは jQuery では実行できません。JavaScript で実行する必要があります。私は使用すべきではないことを知っているdocument.write
ので、誰かがより良い推奨事項を持っているなら、彼らは大歓迎です.
提供されたヒント、提案、または批評を前もって感謝します。