我知道這是一個簡單的任務,但我正在學習JavaScript。我想抓住FOR循環中的第一個項目,並抓住第二個項目。有沒有辦法做到這一點。我只需要三個隨機數組。在FOR循環中選擇特定項目
/**
* Created by the JavaScript Development Team
* Class: PWA
* Goal: Goal7
*/
(function() {
var names = ["walker", 'marisa', 'annie', 'gizmo', 'marvin'];
function shuffle(o) { //v1.0
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
myArray = shuffle(names);
console.log(myArray);
var Person = function (name) {
this.name = name;
console.log(this.name);
// for(i=0; i< names.length; i++){
// var person =[new Person(names[i])];
// }
};
console.log('hello');
var i = 0;
var length = myArray.length = 3
while (i < length) {
//console.log(myArray[i]);
var person = new Person(myArray[i]);
// var person = [new Person(names[i])];
i++;
}
})();