2016-01-04 233 views
1

在下面的示例中,我试图动态地将星期数附加到某个人。如果周数是“5”,我想写出名字“Jeppe”。 https://jsfiddle.net/wgw8yhnL/Javascript - 将数组值赋值给名称

这意味着如果我添加或删除一个人到“学生”数组它仍然将值匹配到当前的人数。

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

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

欲用数字1,5与数字2,6与数字3匹配的名称,以及这样 “叶普”号码,9 “托米”,10 “勒” ,7,11 “山猫”与数字4,8,12

希望你能帮助我:)

+7

听起来像一个基本的'for'循环给我。你熟悉[模数运算符](http://stackoverflow.com/questions/8900652/what-does-do-in-javascript)吗? – Blazemonger

+2

你尝试过什么吗? –

+0

我想输出一个名字。如果周数是“5”,应该是“Jeppe”。像这篇文章只是在CSS中的第n个。 https://css-tricks.com/how-nth-child-works/ – Giefdonut

回答

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/

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); 
}); 
+1

您可以在这里使用'students.length'而不是'4':'(week - 1 )%4' – nils

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 
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); 

新的生产线,只是对代码的缘故警报,当然....

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