0

I have a nested array that holds a handful of coordinates. I am trying to loop the nested array and log the values of each coordinate.

I can get it working in Python, but I need it in JavaScript. Here is how it works in python:

list_of_coordinates = [(100, 98), (200, 78)]
for i in list_of_coordinates:
    x_coord = i[0]
    y_coord = i[1]
    print(x_coord, y_coord)

The output of the above is:

100 98
200 78

When I try to do this in Javascript I get the output of 0 and 1... not the actual coordinate values.

let listOfCoordinates = [[100, 0], [200, 1]];
for (const i in listOfCoordinates) {
    let xCoord = i[0];
    let yCoord = i[1];
    console.log(xCoord, yCoord);
}

The output of this is

0 undefined
1 undefined
1
  • 1
    listOrCoordinates.forEach(coord => console.log(coord[0], coord[1])); would also do the same thing Commented Jul 31, 2020 at 17:29

4 Answers 4

3

You should use for..of instead as the following:

let listOfCoordinates = [[100, 0], [200, 1]];
for (const i of listOfCoordinates) {
    let xCoord = i[0];
    let yCoord = i[1];
    console.log(xCoord, yCoord);
}

See from the documentation:

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.

Sign up to request clarification or add additional context in comments.

1 Comment

haha. Amazing. I've spent the past 30mins googling this. Thank you!
0

forEach way, with deconstruction of the input.

let listOfCoordinates = [[100, 0], [200, 1]];

listOfCoordinates.forEach(([xCoord, yCoord]) => console.log(xCoord, yCoord));

Comments

0

Correct way, if you want to continue to use for...in:

const listOfCoordinates = [[100, 0], [200, 1]];
for (let i in listOfCoordinates) {
    let xCoord = listOfCoordinates[i][0];
    let yCoord = listOfCoordinates[i][1];
    console.log(xCoord, yCoord);
}

Because you named the variable "i", which is often short for "index", I thought you'd want it to be an index, which would use the "in" form.

The for...of form returns the values. It could be written like so:

const listOfCoordinates = [[100, 0], [200, 1]];
    for (let coord of listOfCoordinates) {
        let xCoord = coord[0];
        let yCoord = coord[1];
        console.log(xCoord, yCoord);
    }

Comments

0

You could take for ... of statement and a destructuring assignment to the wanted variables.

let listOfCoordinates = [[100, 0], [200, 1]];

for (const [xCoord, yCoord] of listOfCoordinates) {
    console.log(xCoord, yCoord);
}

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.