Javascript 在javascript中将坐标存储在数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7030229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:19:44  来源:igfitidea点击:

storing coordinates in array in javascript

javascriptarraysgoogle-maps

提问by Sara

I want to store coordinates into an array in javascript, I am new to javascript and do not have an idea how to do it.

我想在 javascript 中将坐标存储到一个数组中,我是 javascript 新手,不知道该怎么做。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by jfriend00

There are a number of ways to store x,y coordinates:

有多种存储 x,y 坐标的方法:

Option 1 (every other index in an array):

选项 1(数组中的所有其他索引):

function storeCoordinate(x, y, array) {
    array.push(x);
    array.push(y);
}

var coords = [];
storeCoordinate(3, 5, coords);
storeCoordinate(19, 1000, coords);
storeCoordinate(-300, 4578, coords);

coords[0] == 3   // x value (even indexes)
coords[1] == 5   // y value (odd indexes)

// to loop through coordinate values
for (var i = 0; i < coords.length; i+=2) {
    var x = coords[i];
    var y = coords[i+1];
} 

Option 2 (simple object in an array):

选项 2(数组中的简单对象):

function storeCoordinate(xVal, yVal, array) {
    array.push({x: xVal, y: yVal});
}

var coords = [];
storeCoordinate(3, 5, coords);
storeCoordinate(19, 1000, coords);
storeCoordinate(-300, 4578, coords);

coords[0].x == 3   // x value
coords[0].y == 5   // y value

// to loop through coordinate values
for (var i = 0; i < coords.length; i++) {
    var x = coords[i].x;
    var y = coords[i].y;
} 

回答by Dorpsidioot

Well, let's say we make it simple, you want to store co?rdinates, so we have x and y:

好吧,假设我们简单点,你想存储坐标,所以我们有 x 和 y:

function coordinate(x, y) {
    this.x = x;
    this.y = y;
}

This is how you create Objects in javascript, they act like functions. With this function you can create your coordinates. Then all you need to do is create an array:

这就是您在 javascript 中创建对象的方式,它们的作用类似于函数。使用此功能,您可以创建坐标。然后你需要做的就是创建一个数组:

var arr = new Array();
arr.push(new coordinate(10, 0));
arr.push(new coordinate(0, 11));

That's it basically

基本上就是这样

回答by Wes

These answers are not usable if you're trying to store a grid/matrix that you wanted to access data point by x,y values later.

如果您尝试存储稍后要通过 x,y 值访问数据点的网格/矩阵,这些答案将不可用。

var coords = [];

for(y=0; y < rows; y++){
   for(x=0;x<cols; x++){
      if(typeof coords[x] == 'undefined']){
         coords[x] = [];
      }

      coords[x][y] = someValue;
   }
}

//accessible via coords[x][y] later

回答by Swapnil

The push method would do the job:

push 方法可以完成这项工作:

var arr = new Array();

arr.push({ x : x_coordinate, y : y_coordinate });

var arr = new Array();

arr.push({ x : x_coordinate, y : y_coordinate });

You can then access them by using

然后,您可以通过使用访问它们

arr[0].x(gives the x coordinate)

arr[0].x(给出 x 坐标)

and

arr[0].y(gives the y coordinate).

arr[0].y(给出 y 坐标)。

Hope it helps.

希望能帮助到你。