0

I have this array declared. While you can possible tell that I'm playing with Google Maps, the contents of this array are not really important, it's just an array.

var mapData =[{"id":1,"name":"Love.Fish","address":"580 Darling Street, Rozelle, NSW","lat":-33.861034,"lng":151.171936,"type":"restaurant"},{"id":2,"name":"Young Henrys","address":"76 Wilford Street, Newtown, NSW","lat":-33.898113,"lng":151.174469,"type":"bar"},{"id":3,"name":"Hunter Gatherer","address":"Greenwood Plaza, 36 Blue St, North Sydney NSW","lat":-33.840282,"lng":151.207474,"type":"bar"},{"id":4,"name":"The Potting Shed","address":"7A, 2 Huntley Street, Alexandria, NSW","lat":-33.910751,"lng":151.194168,"type":"bar"},{"id":5,"name":"Nomad","address":"16 Foster Street, Surry Hills, NSW","lat":-33.879917,"lng":151.210449,"type":"bar"},{"id":6,"name":"Three Blue Ducks","address":"43 Macpherson Street, Bronte, NSW","lat":-33.906357,"lng":151.263763,"type":"restaurant"},{"id":7,"name":"Single Origin Roasters","address":"60-64 Reservoir Street, Surry Hills, NSW","lat":-33.881123,"lng":151.209656,"type":"restaurant"},{"id":8,"name":"Red Lantern","address":"60 Riley Street, Darlinghurst, NSW","lat":-33.874737,"lng":151.21553,"type":"restaurant"}];

Now later on, I want to get some information out of each of the objects in that array and I do so with a for in loop like so:

for(marker in mapData){
    console.log(mapData[marker]);
}

I understand that according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in,

for..in should not be used to iterate over an Array where index order is important.

But I don't really care about the order, so that being the case, are there any disadvantages to iterating over my array with a for in loop?

Glen Pierce
  • 4,401
  • 5
  • 31
  • 50
  • The [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) loop is probably more appropriate. – nbrooks May 18 '17 at 05:33
  • Just found http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea which is possibly a duplicate, though I mention that I don't care about order, it seems there are some other considerations. – Glen Pierce May 18 '17 at 05:33

1 Answers1

0

You could use .foreach() instead.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach?v=example

Romanow
  • 124
  • 2
  • 6