Kate_Booth
Kate_Booth

Reputation: 25

for loop won’t stop looping and causes web page to crash

My code is looping through the coordinates in a feature collection in order to convert the feature collection to a polyline. However, the loop continues on and on, eventually causing my page to crash. I am a newbie and trying my best! Here’s the code:

var path [];
var networkLines;
for ( var I = 0; i < lines[‘features’][‘0’][‘geometry’][‘coordinates’][‘0’].length; i ++) {
     networkLines = lines[‘features’][‘0’][‘geometry’][‘coordinates’][‘0’][i];
     path.push({lat: parseFloat(networkLines[‘1’], lng:parseFloat(networkLines[‘0’])});
}

Upvotes: 0

Views: 168

Answers (3)

Danny Kovene
Danny Kovene

Reputation: 33

Since I can't post a comment yet... I did notice that the is a missing closing parenthesis on the lat value parseFloat(networkLines[‘1’]), and var i was declared in caps by mistake. so the code becomes

var path [];
var networkLines;
for ( var i = 0; i < lines[‘features’][‘0’][‘geometry’][‘coordinates’][‘0’].length; i ++) {
    networkLines = lines[‘features’][‘0’][‘geometry’][‘coordinates’][‘0’][i];
    path.push({lat: parseFloat(networkLines[‘1’]), lng:parseFloat(networkLines[‘0’])});
}

This could cause weird errors in your code.

Upvotes: 1

Alexander Topalo
Alexander Topalo

Reputation: 36

I and i in JavaScript are different variables. So just change I to small i, because i is NaN, NaN++ === NaN

Upvotes: 0

villu164
villu164

Reputation: 208

var path = [];
var lines = [1, 2, 3];
lines.forEach(function (networkLines){
  path.push({ lat: networkLines });
})

you can do a forEach on an array

lines['features']['0']['geometry']['coordinates']['0'].forEach in your case

Upvotes: 0

Related Questions