In OpenLayers library I create layers like so:
var layer = new ol.layer.Tile({
    source: new ol.source.OSM()
});
...
What I want to do is to add some unique attribute to this layer, or something like:
var layer = new ol.layer.Tile({
    source: new ol.source.OSM(),
    customAttr: 'unique_id'
});
...
so that, later I could do this:
map.getLayers().forEach(function(layer){
    if(layer.customAttr === 'unique_id'){
        doSomething();
    }
});
I do not know how to do this in practice.
Change if(layer.customAttr === 'unique_id') to if(layer.get("customAttr") === 'unique_id') and it should work. See this fiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With