简体   繁体   English

将随机对象推入数组

[英]Push randomed objects on array

I need to random objects on array to "make a battle". 我需要在数组上随机放置对象以进行“战斗”。 The random works perfect, but sometimes repeat objects. 随机效果完美,但有时会重复对象。 Also, I can see my var ARR it's full of undefined. 另外,我可以看到我的var ARR充满了不确定的内容。 I don't understand how I need to do to random perfectly without repeated object and fill with randomed objects. 我不明白如何在没有重复对象的情况下完美随机化并填充随机对象。

var avenger = [
        {id: 1, fullName: "Steve Rogers", avengerName: "Captain America", gender: "Male", city: "New York City", markAv: 10},
        {id: 2, fullName: "Tony Stark", avengerName: "IronMan", gender: "Male", city: "New York City", markAv: 15},
        {id: 3, fullName: "Thor Odinson", avengerName: "Thor", gender: "Male", city: "Los Angeles", markAv: 13},
        {id: 4, fullName: "Bruce Banner", avengerName: "Hulk", gender: "Male", city: "Maryland", markAv: 20},
        {id: 5, fullName: "Clint Barton", avengerName: "Hawkeye", gender: "Male", city: "Los Angeles", markAv: 8},
        {id: 6, fullName: "Natasha Romanoff", avengerName: "Black Widow", gender: "Female", city: "Paris", markAv: 14},
        {id: 7, fullName: "Nick Fury", avengerName: "Nick Fury", gender: "Female", city: "New York City", markAv: 5},
        {id: 8, fullName: "Jaume Serradell", avengerName: "Jaumeserr", gender: "Male", city: "Barcelona", markAv: 18}
    ]

    function avengerPairs(myObject) {

        var arr = [];

        for (var i=0; i<avenger.length; i++) {

            var randomAvenger = avenger[Math.floor(Math.random() * avenger.length)];

            if (randomAvenger[i] !== avenger[i]) {
                arr.push([randomAvenger, avenger[i+1]]);
                i++;    
            }
        }

        console.log(arr);

        for (var i=0; i<arr.length; i++) {

            console.log(Math.max(arr[i][0].markAv, arr[i][1].markAv));

            if (arr[i][0].markAv < arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][1].fullName + " is better!");
            } else if (arr[i][0].markAv === arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => Are equals!");
            } else {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][0].fullName + " is better!");
            }
        }
    }

avengerPairs(avenger);

Your array is being filled with undefined because of these lines: 由于以下几行,您的数组被undefined填充:

var arr = [];

for (var i=0; i<avenger.length; i++) {

    var random = avenger[Math.floor(Math.random() * avenger.length)];

    console.log(random);
    arr.push([random[i], random[i+1]]);
    i++;
}

random successfully creates a random index to look into the avenger array and assigns the corresponding value in the array to the variable random . random成功创建了一个随机索引以查看avenger数组,并将数组中的相应值分配给变量random

You should see this successfully in the following console.log . 您应该在以下console.log成功看到它。

It's the next line that gives you trouble. 这是给您带来麻烦的下一行。 Inside your push call, you try to access two indices on the random variable with random[i] and random[i + 1] . 在推入调用中,您尝试使用random[i]random[i + 1]访问random变量上的两个索引。 This returns undefined, because your random variable is not an array of avengers but a single avenger , and the integer keys don't exist on that object, thus returning undefined . 这将返回undefined,因为您的random变量不是复仇者数组而是一个复仇者 ,并且该对象上不存在整数键,因此返回undefined

I would recommend a better name for your random variable. 我会为您的random变量推荐一个更好的名称。 Perhaps randomAvenger ? 也许randomAvenger That would help make it clear that you're incorrectly indexing into an avenger, instead of an array of avengers. 这将有助于弄清楚您是错误地索引了一个复仇者,而不是复仇者数组。

To successfully pair two avengers, you will need two separate random indexes into the avengers array. 为了成功地将两个复仇者配对,您将需要两个单独的随机索引到avengers数组中。 You'll also want to watch out for selecting the same avenger twice though! 您还需要提防两次选择相同的复仇者!

So I believe the problem is at arr.push([random[i], random[i+1]]) . 所以我认为问题出在arr.push([random[i], random[i+1]])

If you look at the console.log(random) output, you'll realize that random represents an Object of a single avenger (ie { id: 4, fullName: 'Bruce Banner', avengerName: 'Hulk', gender: 'Male', city: 'Maryland',markAv: 20 } ). 如果查看console.log(random)输出,您将意识到random代表单个复仇者的对象(即{ id: 4, fullName: 'Bruce Banner', avengerName: 'Hulk', gender: 'Male', city: 'Maryland',markAv: 20 } )。

That means random[i] and random[i+1] are both undefined. 这意味着random[i]random[i+1]都未定义。

My suggestion is to do arr.push([random, avenger[i+1]]); 我的建议是做arr.push([random, avenger[i+1]]); .

You could also use Array.prototype.splice to remove each avenger as you pair them up, so there are no duplicates. 您还可以使用Array.prototype.splice在配对每个复仇者时将其删除,因此没有重复项。

Okay a few hours later and I went ahead and got it to work with splicing / without repeats. 好的,几个小时后,我继续进行拼接处理(没有重复)。 I left in a bunch of commented out console.log statements if you want to follow along. 如果您想继续,我留下了一堆注释掉的console.log语句。

 var avenger = [ {id: 1, fullName: "Steve Rogers", avengerName: "Captain America", gender: "Male", city: "New York City", markAv: 10}, {id: 2, fullName: "Tony Stark", avengerName: "IronMan", gender: "Male", city: "New York City", markAv: 15}, {id: 3, fullName: "Thor Odinson", avengerName: "Thor", gender: "Male", city: "Los Angeles", markAv: 13}, {id: 4, fullName: "Bruce Banner", avengerName: "Hulk", gender: "Male", city: "Maryland", markAv: 20}, {id: 5, fullName: "Clint Barton", avengerName: "Hawkeye", gender: "Male", city: "Los Angeles", markAv: 8}, {id: 6, fullName: "Natasha Romanoff", avengerName: "Black Widow", gender: "Female", city: "Paris", markAv: 14}, {id: 7, fullName: "Nick Fury", avengerName: "Nick Fury", gender: "Female", city: "New York City", markAv: 5}, {id: 8, fullName: "Jaume Serradell", avengerName: "Jaumeserr", gender: "Male", city: "Barcelona", markAv: 18} ] function avengerPairs(myObject) { var arr = []; var lengthSave = avenger.length for (var i=0; i<lengthSave; i++) { var newLength = avenger.length var index = Math.floor(Math.random() * newLength) var randomAvenger = avenger[index]; var pairArr = (avenger.splice(index, 2)) // console.log(avenger.length) // console.log(pairArr.length) if (pairArr.length < 2 ) { var anotherPair if (avenger.length > 1) { anotherPair = avenger.splice(index, 1)[0] } else if (avenger.length === 1) { anotherPair = avenger.splice(0, 1)[0] } // console.log(!!anotherPair) // console.log(anotherPair) // console.log('another pair') if (!!anotherPair === true) { pairArr.push(anotherPair) } } // console.log(pairArr) // console.log(!!pairArr[0]) // console.log(!!pairArr[1]) // console.log('pairArr') if (!!pairArr[0] === true && !!pairArr[1]) { arr.push(pairArr) } } // console.log(arr); for (var i=0; i<arr.length; i++) { // console.log(Math.max(arr[i][0].markAv, arr[i][1].markAv)); if (arr[i][0].markAv < arr[i][1].markAv) { console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][1].fullName + " is better!"); } else if (arr[i][0].markAv === arr[i][1].markAv) { console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => Are equals!"); } else { console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][0].fullName + " is better!"); } } } avengerPairs(avenger); 

Random works perfectly, but I can't random avenger with randomAvenger to prevent repeated values. 随机工作完美,但我不能使用randomAvenger进行随机复仇者以防止重复值。 Now, between two values into objects in array [a,b] [a,b] it's perfect, but between values to objects, continues repeating values. 现在,在数组[a,b] [a,b]中对象的两个值之间是完美的,但是在对象的值之间继续重复值。

That's my code new code: 那是我的代码新代码:

var avenger = [
        {id: 1, fullName: "Steve Rogers", avengerName: "Captain America", gender: "Male", city: "New York City", markAv: 10},
        {id: 2, fullName: "Tony Stark", avengerName: "IronMan", gender: "Male", city: "New York City", markAv: 15},
        {id: 3, fullName: "Thor Odinson", avengerName: "Thor", gender: "Male", city: "Los Angeles", markAv: 13},
        {id: 4, fullName: "Bruce Banner", avengerName: "Hulk", gender: "Male", city: "Maryland", markAv: 20},
        {id: 5, fullName: "Clint Barton", avengerName: "Hawkeye", gender: "Male", city: "Los Angeles", markAv: 8},
        {id: 6, fullName: "Natasha Romanoff", avengerName: "Black Widow", gender: "Female", city: "Paris", markAv: 14},
        {id: 7, fullName: "Nick Fury", avengerName: "Nick Fury", gender: "Female", city: "New York City", markAv: 5},
        {id: 8, fullName: "Jaume Serradell", avengerName: "Jaumeserr", gender: "Male", city: "Barcelona", markAv: 18}
    ]

    function avengerPairs(myObject) {

        var arr = [];

        for (var i=0; i<avenger.length; i++) {

            var randomAvenger = avenger[Math.floor(Math.random() * avenger.length)];


            if (randomAvenger !== avenger[i]) {
                arr.push([randomAvenger, avenger[i+1]]);
                i++;    
            } else {
                console.log("Not equals");
            }   
        }

        console.log(arr);

        /*
        for (var i=0; i<arr.length; i++) {

            console.log(Math.max(arr[i][0].markAv, arr[i][1].markAv));

            if (arr[i][0].markAv < arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][1].fullName + " is better!");
            } else if (arr[i][0].markAv === arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => Are equals!");
            } else {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][0].fullName + " is better!");
            }
        }
        */
    }

avengerPairs(avenger);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM