如何在jquery中将数组推送到另一个数组中

时间:2016-03-05 16:49:50

标签: javascript jquery arrays

我想创建一个如下所示的数组。

var path = [
  [41.02178, 29.26108],
  [41.02196, 29.26067],
  [41.02251, 29.26031],
  [41.02258, 29.26015],
  [41.02267, 29.25926]
];

在我的代码中我尝试了这个:

var loc =[];
for (var i = 0; i < result.ResponseData.length; i++) 
{
   loc = [result.ResponseData[i].Latitude, result.ResponseData[i].Longitude];
   path.push(loc);
}

但这是结果:

path=[0,0,29.26108,41.02178,29.26067,41.02196,29.26031,41.02251];

1 个答案:

答案 0 :(得分:-2)

您的代码是正确的,我认为是您打印路径的方式。

路径包含您想要的数组数组,您可以使用console.log(path)找到它,并获得:

Array [ Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2] ]

&#13;
&#13;
var path = [
                  [41.02178, 29.26108],
                  [41.02196, 29.26067],
                  [41.02251, 29.26031],
                  [41.02258, 29.26015],
                  [41.02267, 29.25926]
                ];


var loc = [];
      for (var i = 0; i < 5; i++) 
      {
         loc = [1, 1];
         path.push(loc);
      }

console.log(path);

console.log("[[" + path.join("],\n[") + "]]");
&#13;
&#13;
&#13;