-1

Consider an array1 of n no. of features

array1=[feat1,feat2,...,featn]

and array2 of m no. of linearRings

array2=[ring1,ring2,...,ringm].

Now write a program in javascript such that every element of array2 is compared with every element of array1.

PS: please suggest an approach apart from nested for loop.
The approach I tried:

features.map(feature => {
  linearRings.map(linearRing => {
    const singleFeature = getTurfFeature(feature);
    const pointOfLinearRing = point(
      transform(linearRing.getFirstCoordinate(), 'EPSG:3857', 'EPSG:4326')
    );
    const checkForOverlap = booleanIntersects(pointOfLinearRing, singleFeature);
    checkForOverlap && feature.getGeometry().appendLinearRing(linearRing);
  });
});
brk
  • 48,835
  • 10
  • 56
  • 78

1 Answers1

0

You can use Set & store the value of the first array here. Then you can check the result of point function and verify if it is present in set

const array1 = ['feat1', 'feat2', 'featn']
const set = new Set();
const array2 = ['ring1', 'ring2', 'ringm'];

array1.forEach(feature => {
  const singleFeature = getTurfFeature(feature);
  if (!set.has(singleFeature)) {
    set.add(singleFeature)
  }
})

array2.map(linearRing => {

  const pointOfLinearRing = point(
    transform(linearRing.getFirstCoordinate(), 'EPSG:3857', 'EPSG:4326')
  );
  // do whatever you want to do here
  return set.has(pointOfLinearRing);
});
brk
  • 48,835
  • 10
  • 56
  • 78