简体   繁体   English

Javascript-将数组值分配给名称

[英]Javascript - Assign array value to name

In the example below im trying to attach the week number to a person dynamically. 在下面的示例中,im试图将周号动态附加到某人。 If the week number is "5" i want to write out the name "Jeppe". 如果星期数是“ 5”,我想写出名称“ Jeppe”。 https://jsfiddle.net/wgw8yhnL/ https://jsfiddle.net/wgw8yhnL/

That means if i add or remove a person to the "students" array it will still match values to the current numbers of persons. 这意味着,如果我在“学生”数组中添加或删除一个人,它将仍然将值与当前人数相匹配。

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

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

I want to match the names, with numbers like this "Jeppe" with the numbers 1, 5, 9 "Tommy" with the numbers 2, 6, 10 "Rene" with the numbers 3, 7, 11 "Charlotte" with the numbers 4, 8, 12 我想将名称与数字类似,例如“ Jeppe”,数字1、5、9“ Tommy”和数字2、6、10“ Rene”,数字3、7、11“ Charlotte”和数字4、8、12

Hope you can help me :) 希望你能帮我 :)

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/ https://jsfiddle.net/wgw8yhnL/2/

Simply get the index in the students array based on the current week, you don't even need the weeks array. 只需根据当前星期在students数组中获取索引,您甚至不需要weeks数组。

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

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

alert(student);

Here is an improved demo: https://jsfiddle.net/oL2otkj9/2/ 这是一个改进的演示: 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);
});

maybe this helped you 也许这对你有帮助

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

I don't understand very clearly the kind of output you are looking for, and don't understand either why the weeks array is made of strings. 我不太清楚您要查找的输出类型,也不理解为什么Weeks数组是由字符串组成的。 Could that array have other values than 1, 2, ..., 12? 该数组可以具有除1,2,...,12以外的其他值吗? String values? 字符串值? Anyway, if what you are looking for is a matching code, a thing like this would be enough: 无论如何,如果您正在寻找的是匹配的代码,那么像这样的事情就足够了:

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

alert(strRet);

The new line and the alert just for the sake of the code, of course.... 当然,新行和警报只是为了代码而已。

Using students as a struct you can easily display: 使用学生作为结构,您可以轻松显示:

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

for given: 给定的:

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

See this JSFiddle 看到这个JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM