1

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ので、誰かがより良い推奨事項を持っているなら、彼らは大歓迎です.

提供されたヒント、提案、または批評を前もって感謝します。

4

1 に答える 1

1

2 つの別々の配列を保持する代わりに、年を配列要素自体に関連付けることができます。text証言を保持する1 つのプロパティを使用して配列要素にオブジェクトを格納できますが、YYYY実際の年のプレースホルダーとyearHired、その人物が雇用された年を保持する2 番目のプロパティを使用できます。

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}

この方法では、配列から 1 つの要素のみを選択し、勤続年数を計算して、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 に答える