2016-01-04 27 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