在阵列上推送randomed对象

时间:2018-04-25 21:45:49

标签: javascript arrays object

我需要在数组上随机对象来“制造战斗”。随机工作完美,但有时重复对象。此外,我可以看到我的var ARR它充满了未定义。我不明白如何在没有重复对象的情况下完美随机地填充randomed对象。

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

3 个答案:

答案 0 :(得分:3)

由于以下几行,您的数组正在填充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成功创建一个随机索引以查看avenger数组,并将数组中的相应值分配给变量random

您应该在以下console.log中成功查看此内容。

下一行会给你带来麻烦。在您的推送调用中,您尝试使用randomrandom[i]访问random[i + 1]变量上的两个索引。这将返回undefined,因为您的random变量不是复仇者的数组,而是单个复仇者,并且该对象上不存在整数键,因此返回undefined

我会为您的random变量推荐一个更好的名称。也许randomAvenger?这将有助于清楚地表明你错误地索引到复仇者,而不是一群复仇者。

要成功配对两个复仇者,您需要在avengers数组中使用两个单独的随机索引。你也要注意两次选择同一个复仇者!

答案 1 :(得分:1)

所以我认为问题发生在arr.push([random[i], random[i+1]])

如果查看console.log(random)输出,您会发现random代表单个复仇者的对象(即{ id: 4, fullName: 'Bruce Banner', avengerName: 'Hulk', gender: 'Male', city: 'Maryland',markAv: 20 })。

这意味着random[i]random[i+1]都是未定义的。

我的建议是arr.push([random, avenger[i+1]]);

您还可以使用Array.prototype.splice删除每个复仇者,因此没有重复。

好几个小时之后,我继续前进,让它与拼接/无重复一起工作。如果你想跟随,我会留下一堆已注释掉的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);

答案 2 :(得分:0)

随机效果很好,但我无法随机随机复仇以防止重复值。现在,在数组[a,b] [a,b]中的两个值之间,它是完美的,但在值与对象之间,继续重复值。

这是我的代码新代码:

my_var="foobar"
json=`cat <<<{\"quicklock\":\"${my_var}\"}`