Get single layer in OpenLayers 3

I finally made it with a little extension of the ol.Map object:

if (ol.Map.prototype.getLayer === undefined) {    
    ol.Map.prototype.getLayer = function (id) {
        var layer;
        this.getLayers().forEach(function (lyr) {
            if (id == lyr.get('id')) {
                layer = lyr;
            }            
        });
        return layer;
    }
}

This piece of code has to be executed after the OL3 library is loaded. As it uses only public functions of the ol.Map object, it is usable also with the compiled version of the OL3 library.


This is currently the case, see: https://github.com/openlayers/ol3/issues/2907 for the relevant discussion