Javascript - 将数组值分配给name

时间:2016-01-04 14:56:41

标签: javascript arrays

在下面的示例中,我尝试动态地将周数附加到人。如果周数是“5”,我想写出名称“Jeppe”。 https://jsfiddle.net/wgw8yhnL/

这意味着如果我向“学生”数组添加或删除某个人,它仍然会将值与当前人数匹配。

var weeks = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];

var students = ["Jeppe", "Tommy", "Rene", "Charlotte"];

我希望将这些名称与这样的数字相匹配 “Jeppe”,数字1,5,9 “汤米”号码为2号,6号,10号 “Rene”的数字为3,7,11 “夏洛特”号码为4,8,12

希望你能帮助我:)。

5 个答案:

答案 0 :(得分:1)

Date.prototype.getWeek = function() {
  // Create a copy of this date object  
  var target = new Date(this.valueOf());

  // ISO week date weeks start on monday  
  // so correct the day number  
  var dayNr = (this.getDay() + 6) % 7;

  // ISO 8601 states that week 1 is the week  
  // with the first thursday of that year.  
  // Set the target date to the thursday in the target week  
  target.setDate(target.getDate() - dayNr + 3);

  // Store the millisecond value of the target date  
  var firstThursday = target.valueOf();

  // Set the target to the first thursday of the year  
  // First set the target to january first  
  target.setMonth(0, 1);
  // Not a thursday? Correct the date to the next thursday  
  if (target.getDay() != 4) {
    target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
  }

  // The weeknumber is the number of weeks between the   
  // first thursday of the year and the thursday in the target week  
  return 1 + Math.ceil((firstThursday - target) / 604800000); // 604800000 = 7 * 24 * 3600 * 1000  
}

function getName(weeknr, students) {
  mod = weeknr % students.length;
  console.log(mod);
  return students[mod];

}

var today = new Date();
var students = ["Jeppe", "Tommy", "Rene", "Charlotte"];

console.log(getName(today.getWeek(), students));

https://jsfiddle.net/wgw8yhnL/2/

答案 1 :(得分:0)

根据当前周,只需获取students数组中的索引,您甚至不需要weeks数组。

var week = 6;   
var students = ["Jeppe", "Tommy", "Rene", "Charlotte"];

var index = (week - 1) % 4
var student = students[index]

alert(student);

以下是一个改进的演示:https://jsfiddle.net/oL2otkj9/2/

var students = ["Jeppe", "Tommy", "Rene", "Charlotte"];

function getStudentName(week) {
    var index = (week - 1) % students.length;
    return  students[index];
}

var weekInput = document.getElementById('week');
var output = document.getElementById('student');

weekInput.addEventListener('change', function() {
    output.innerHTML = getStudentName(this.value);
});

答案 2 :(得分:0)

也许这有助于你

var students = JSON.stringify({
  Jeppe: [1,5,9],
  Tommy: [2,6,10],
  Rene: [3,7,11],
  Charlotte: [4,8,12]
});
var needle = '1'; // find Jeppe
var part = students.slice(0, students.indexOf(needle));
var lastIndex = part.lastIndexOf('"');
var name = part.slice(part.slice(0, lastIndex).lastIndexOf('"') + 1, lastIndex); // returns Jeppe

答案 3 :(得分:0)

我不太清楚你正在寻找的输出类型,也不明白为什么周数组是由字符串组成的。那个数组可以有除1,2,...,12之外的其他值吗?字符串值?无论如何,如果您正在寻找的是匹配的代码,那么这样就足够了:

var strRet = "";
for(var i = 1; i < 13; i++)
{
    strRet += i + ": ";
    var index = (i - 1) % 4;
    strRet += students[index] + "\n";
}

alert(strRet);

新线和警报只是为了代码,当然......

答案 4 :(得分:0)

使用学生作为结构,您可以轻松显示:

{"A":[1,4,7,10],"B":[2,5,8,11],"C":[3,6,9,12]}

给出:

var students = {"A": [], "B": [], "C":[]};
var weeks = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];

请参阅此JSFiddle