【问题标题】:javascript - array displaying two different arrays in loggingjavascript - 在日志记录中显示两个不同数组的数组
【发布时间】:2015-11-22 06:14:13
【问题描述】:

我对我的 javascript 代码中发生的事情感到非常困惑。我正在尝试编写一个函数来创建一个随机点数组。但是,当我在循环内记录数组(其功能是向数组添加点),然后在循环外再次记录它时,我会打印出两个不同的数组。

第一个,如预测的随机 X,Y 点列表,但第二个日志仅包含最后输入的 X,Y 点。

function Pick()
{
   var numPoints = 4 * Math.floor(4+Math.random()*5);
   var chosen_pts = [];
   var lastPt;
   for(var i = 0;i<numPoints;i++)
   {
       if(chosen_pts.length==0)
       {
           var temp = new Point(Math.floor(Math.random()*250),Math.floor(Math.random()*250));
           var newPt = pickClose(temp);
           chosen_pts.push(newPt);
           lastPt = newPt;
       }
       else{
          var newPt = pickClose(lastPt);
          chosen_pts.push(newPt); 
          }
        console.log(chosen_pts[i]); //LINE 106

   }
   console.log("\noutside of the loop:")
   for(var i = 0;i<numPoints;i++)
   {
      console.log(chosen_pts[i]);   //LINE 111
   }
}

查看控制台照片 Console Array 1 Console Array 2

编辑:

function pickClose(lastPt)
{
        var x = lastPt["X"];
        var y = lastPt["Y"];
        var dx = 0;
        var dy = 0;
        var rand = Math.floor(1+Math.random()*100);

        if(rand<50){
            dx = 1+Math.floor(Math.random()*10);
            dy = 1+Math.floor(Math.random()*10);
            if( (dx+dy)%3==0 ){
                dx*=-1;
            }
        }
        else if(rand<80)
        {
            dx = 1+Math.floor(Math.random()*25);
            dy = 1+Math.floor(Math.random()*25);
            if( (dx+dy)%3==0 ){
                dy*=-1;

            }
        }
        else{
            dx = 1+Math.floor(Math.random()*60);
            dy = 1+Math.floor(Math.random()*60);
            if( (dx+dy)%4==0 ){
                dx*=-1;
                dy*=-1;

            }
        }
        if( (x+dx) < 500&& (x+dx) >=0 )
            lastPt["X"]+=dx;
        else
            lastPt["X"]-=dx;


        if( (y+dy) < 500&& (y+dy) >=0 )
            lastPt["Y"]+=dy;
        else
            lastPt["Y"]-=dy;

        return lastPt;

  }

看起来很乱,但本质上我希望根据初始随机数从 for(dx,dy) 中随机选择不同范围的值。

【问题讨论】:

  • pickClose 函数似乎是罪魁祸首。请附上它的源代码。
  • 考虑到第一次调用 console.log 它会为每个点打印不同的数字,我认为这不是问题

标签: javascript arrays syntax duplicates artificial-intelligence


【解决方案1】:

pickClose 函数总是返回传递的元素。 javascript 中的对象通过引用传递,因此您稍后对对象所做的任何更改也将应用于您存储的对象的所有其他引用。

澄清一下:

var point1 = new Point(1, 2);
var point2 = pickClose(point1);
// inside pickClose, parameter lastPt = point1:
  lastPt["X"] += dx; // <- this also alters point1!
  lastPt["Y"] += dy; // <- this also alters point1!

所以如果你想在你的函数中返回一个 new Point(而不是改变传递的那个),你必须创建一个你改变并返回的新对象:

var newX = x, newY = y;
// instead of:
lastPt["X"]+=dx;
// do:
newX += dx;

// then, at the bottom, instead of
return lastPt;
// create a new instance
return new Point(newX, newY);

【讨论】:

  • @AaronGoldsmith 没问题!如果答案解决了您的问题,请将其标记为已接受,因此它不会显示为“等待答案”。
【解决方案2】:

您的 pickClose 函数将 lastPt 作为参考,因此您正在修改数组中已经存在的 Point 并再次添加它。

尝试将第 103 行更改为:

var newPt = pickClose(new Point(lastPt.X, lastPt.Y));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多