-1

I'm trying to loop through this array

const test = [(18, 20), (45, 2), (61, 12), (37, 6), (21, 21), (78, 9)];

but it's output looks like this

console.log(test); //[20, 2, 12, 6, 21, 9]

please let me know how to loop through this array correctly .

danh
  • 62,181
  • 10
  • 95
  • 136

1 Answers1

1

The problem isn't the looping (though you haven't shown any looping in your question). Your array starts out with just the values 20, 2, 12, etc. in it. (18, 20) evaluates to 20, because the comma operator evaluates its left-hand operand (18), throws that away, and evaluates its right-hand operand and takes that value as its result.

If you wanted an array of tuples, you wanted [], not (), on the inner arrays:

const test = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875