17

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.

Any help would be appreciated.

6 Answers 6

27

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

Option 1 (every other index in an array):

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

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;
} 
Sign up to request clarification or add additional context in comments.

2 Comments

how to put 3 coordinates and retrieve them back using for loop
@Shaheryar - Updated to add three coordinates and loop through them when done.
5

Well, let's say we make it simple, you want to store coördinates, so we have x and 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:

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

That's it basically

3 Comments

Thankks how to retrieve them back from array...like using a for loop.
Simple, for loop! for (var = 0; i < arr.length; i++) { alert(arr[i].x); }
Awesome, thanks! I used this along with the js some() method, to only add unique points to my array like if(!arr.some(arr => arr.x+','+arr.y === x+','+y)){ arr.push(new coordinate(x, y)); } (Credit also to TechRepublic)
2

The push method would do the job:

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)

and

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

Hope it helps.

Comments

1

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.

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

1 Comment

Not true. You can check for the existence of specific points using the js some() method. See my comment above. (I found the [x][y] method to have other messy issues.)
0

I saw a way to store coordinates using a Set. By using a Set you can store unique coordinates which can come in handy at times.

let coords = new Set();
coords.add(x + "," + y)

Comments

0
//creates a new point coordinate(x,y) - in this case its coordinate (col,row)
function coord(myColumn, myRow) {
    return { col: myColumn, row: myRow}
}

 // creates an array of coord
var myCoords = [
    coord(0, 0), 
    coord(0, -2), coord(+1, -1),
    coord(+1, +1), coord(0, +2),
    coord(-1, +1), coord(-1, -1),
]

// access by - add coords and return a new one
function add(coordStart, coordEnd) {
return coord(coordStart.col + coordEnd.col, coordStart.row + coordEnd.row)
}

// call function
let myNewCoord = add(myCoords[3], myCoords[0])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.