Search code examples
leafletturfjs

Get access to each element in object using push Leaflet and turfjs


I succeed to get intersection between line and polygon and display it on map. I had already post this issue here. Now I'm trying to display result for each line on console. When I tried to write console.log(result[i]) I got undefined. What is the right syntax I have to do, I tried many times. Here is my current code:

var lines = [line1, line2, line3, line4];
for (var i = 0; i < lines.length; i++) {
  var intersection = [];
  var result = [];
  intersection = turf.intersect(lines[i], polygon1);
  if (intersection) {
    result.push(intersection);
    L.geoJson(result, {
      style: Style
    }).addTo(map);
    console.log(JSON.stringify(result[i]));
  } else {
    L.geoJson(lines[i]).addTo(map);
  }


Solution

  • result is being defined inside your for loop which operates over lines...so why are you trying to use that loop's internal variable(meant for lines) on result, which only receives input from turf. I would think you just need console.log(result[0]), which would log your intersection.Secondly, I don't see the benefit of calling JSON.stringify for that console.log.