简体   繁体   English

JavaScript:为什么array.push()追加两个对象而不是一个

[英]JavaScript: why array.push() appends two objects instead of one

I am quite new to JS and there is a problem with my code that I really don't understand: I try to append a text-value pair to an array and basically it works, but instead of appending a single pair, it appends two pairs. 我对JS还是很陌生,我的代码确实有一个我不明白的问题:我尝试将文本值对添加到数组中,并且基本上可以使用,但是不是添加单个对,而是添加两个对。 Below is the code snippet and the problem is in the latter if-statement. 下面是代码片段,问题出在后一个if语句中。 I have added comments on to the snippet to explain what I am doing. 我已在代码段中添加了评论,以解释我在做什么。

 players.addResult = function(result){

 //result-argument contains results of games, like: Object {0: "4-2", 1: "5-3", 2: "7-1"}


    var playerList = [];        
    var standingArray = [];

    resultLen = Object.keys(result).length;

    //loop iterates thru results by index
    for (var x=0; x < resultLen; x++) {
        var newResult = result[x];
        newResult = newResult.split("-");

        if (newResult[0] > newResult[1]) {

            //the next line retrieves the name of a winning player from another array
            playerVal = players.pairs[x].player1;
            playerVal = playerVal.text;
            console.log(playerVal); //Output of this: Bill
            value = playerList.indexOf(playerVal); // Checks if the player already exists on the standings, returns -1 if false.

            if (value === -1) {

          /*if the player gets his first points, his name and earned
          points are added on to the standingArray. And the next line of code is the
          interesting one, because I am expecting to have an array with
          index, name of the winner and points. Like this: 
          Object 0 name:"Bill" points:2. But  instead of that, there are two                 
          objects on the array, one for both players: 
          [Object]
           0: Object
             name:  "Bill"
             points: 2
             __proto__: Object
           1: Object
             name: "Greg"
             points: 2
             __proto__: Object
          length: 2
          __proto__: Array[0]*/

                standingArray.push({name: playerVal, points: 2})

                playerList.push(playerVal); //this is for keeping on track that player is now on the standingArray

                console.log(playerVal);
                console.log(playerList);
                console.log(standingArray);
            }
            else {
                console.log("else");
            }               
        console.log(playerList);
        console.log(standingArray);

So the question is that how JS gets Greg's name - I have checked the playerVal-variable and it really contains only Bill's name. 所以问题是JS如何获得Greg的名字-我检查了playerVal-variable,它实际上只包含Bill的名字。 Any help is warmly welcome! 热烈欢迎任何帮助!

The output that appears in the console from console.log is alive. console.log出现在控制台中的输出仍然有效。 It reflects changes to the objects passed to console.log after the console.log call has occurred. 它反映了在进行console.log调用之后传递给console.log的对象的更改。

You might want to JSON.stringify before passing the object to console.log or make a copy of the Array: 您可能需要JSON.stringify传递给console.log或复制Array:

console.log(playerList.slice());
console.log(standingArray.slice());

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

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