Class: SyncArrays service:ngeoSyncArrays


new SyncArrays()

Provides a function that synchronizes two arrays, arr1 and arr2. arr2 is a subset of arr1, it includes the elements of arr1 that passes the filter. When elements are added to/removed from arr1, arr2 is updated to include the elements of arr1 that pass the filter. When the order of elements in arr2 changes, arr1 is reordered to have the same order as arr2.

This can for example be used to synchronize the array of layers in the map with the array of selected layers, where layers may be added to/removed from the map, and the order of selected layers may change.

let dereg = ngeoSyncArrays(map.getLayers().getArray(), selectedLayers,
    true, scope, function(layer) {
      // exclude the layer at index 0 in the map
      return map.getLayers().indexOf(layer) !== 0;

This will return a function that can be called to cancel synchronization.