选择FOR循环中的特定项

时间:2013-12-14 22:59:05

标签: javascript

我知道这是一个简单的任务,但我正在学习JavaScript。我想抓住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++;
    }
})();

4 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

for (var i=0; i++; i < names.length; i++) {
       if (i==1) {
          //grab
       }
       else if (i==2) {
          //grab
       }
       else {
          //Do more
       }
}

甚至更好:names[0]是数组中的第一个,names[1]是第二个。如果这就是你所需要的,那就不需要经过它了。

答案 1 :(得分:0)

你的意思是

for(var i = 2; i >=0; i--) {
    new Person(myArray[i]);
}

但老实说,如果你知道你想要多少物品并且这些物品已经被随机化,你为什么需要循环?

答案 2 :(得分:0)

在while循环中使用break:

(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])];
              if(i == 2){ break; } // <==
            i++;
        }
    })();

答案 3 :(得分:0)

我觉得你有点过于复杂了。如果你只想要三个随机的名字,你为什么要把所有的名字洗牌?我为你写了一些代码,我觉得它更清晰。

(function () {

    var people = ['walker', 'marisa', 'annie', 'gizmo', 'marvin'];

    var random_people_to_get = 3;
    var random_people = [];

    var Person = function (name) { this.name = name; };


    for( var i = 0; i < random_people_to_get; i++ ){

      // Grabbing a random name from people and removing it!
      var random_name = people.splice( Math.floor(Math.random() * people.length), 1)[0];

      // Make the random_name into a person and put it into the random_people array!
      random_people.push(new Person(random_name));

    }

    console.log(random_people);

})();

如果你想坚持自己的方式,可以使用slice制作包含前三项的数组。就像:

var firstThreeRandomNames = myArray.slice(0, 3);