简体   繁体   中英

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. 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:

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 .

You should see this successfully in the following 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] . 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 .

I would recommend a better name for your random variable. Perhaps 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. 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]]) .

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 } ).

That means random[i] and random[i+1] are both undefined.

My suggestion is to do 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.

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.

 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. Now, between two values into objects in array [a,b] [a,b] it's perfect, but between values to objects, continues repeating values.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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