假设其子排列我的课程的数组:Suffle阵列由具有相同的字段值
Courses:
Course:
name: 'Bible'
grade: 87
Course:
name: 'Math'
grade: 87
Course:
name: 'Physics'
grade: 87
Course:
name: 'Biology'
grade: 10
Course:
name: 'Geography'
grade: 10
Course:
name: 'Literature'
grade: 0
我想洗牌的是具有相同等级的子场。
例如,一个结果可能是(我只写了cources名字,但需要整个字段):
Math, Bible, Physics, Geography, Biology, Literature
另一个结果可能是:
Bible, Math, Physics, Biology, Geography, Literature
文学将在最后,导致没有另一个等级等于0.
我有一个功能,可以使数组(无需考虑子课程的成绩):
function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
阵列是:
var courses = [];
courses.push(new Course('Bible', 87));
courses.push(new Course('Math', 87));
courses.push(new Course('Physics', 87));
courses.push(new Course('Biology', 10));
courses.push(new Course('Geography', 10));
courses.push(new Course('Literature', 0));
function Course(name, grade) {
this.name = name;
this.grade = grade;
}
这是我已经创建了一个的jsfiddle:http://jsfiddle.net/Ht6Ym/3844/
任何帮助理解。
可以请你告诉数组 – brk
@ user21813 97,我已经更新了我的主题。谢谢! –
先洗牌,然后按等级分类。 – JJJ