listing of layers with indexof causes issues

1479
5
Jump to solution
01-07-2016 10:46 AM
LefterisKoumis
Occasional Contributor III

This is a rather bizarre behavior when I use the indexof to find layer names that contain "Buffer".

So by running this:

array.forEach(this.map.graphicsLayerIds, function(layerId) {  
          var layer = this.map.getLayer(layerId);  
          console.log(layer.name);
        }, this);  

I get this output:

I modified it to capture only the layer names containing "Buffer", so I run this:

array.forEach(this.map.graphicsLayerIds, function(layerId) {  
          var layer = this.map.getLayer(layerId);
  if((layer.name).indexOf("Buffer") > -1){
  console.log(layer.name);
  }
        }, this);

and I get this:

meaning that the layer is undefined!!!

Ideas?

Thank you.

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  OK, now I see the issue. You have one or more layers that do not have a name specified in your map, so you need to check for the existence of the name property first:

array.forEach(this.map.graphicsLayerIds, function(layerId) {
          var layer = this.map.getLayer(layerId);
          if(layer.name && layer.name.indexOf("Buffer") > -1){
            console.log(layer.name);
          }
        }, this);

View solution in original post

5 Replies
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

   Try this

if(layer.name.toString().indexOf("Buffer") > -1){

0 Kudos
LefterisKoumis
Occasional Contributor III

Thank Robert.

But I got the same error:

array.forEach(this.map.graphicsLayerIds, function(layerId) {  
           layer = this.map.getLayer(layerId);
  mylayer.push(layer);
        }, this);  
  for(j=0;j<mylayer.length; j++){
  if(layer.name.toString().indexOf("Buffer") > -1){ 
           console.log(mylayer.name); 
      }
  }
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  OK, I was not paying close enough attention to your code.

array.forEach(this.map.graphicsLayerIds, function(layerId) {   
  layer = this.map.getLayer(layerId); 
  mylayer.push(layer); 
}, this);   
for(j=0;j<mylayer.length; j++){ 
  if(mylayer.name.toString().indexOf("Buffer") > -1){ 
    console.log(mylayer.name); 
  } 
}
0 Kudos
LefterisKoumis
Occasional Contributor III

Sorry, the problem, persist.

I also have to apologize, that after my first posting I introduced the array mylayer to see if I needed to push the layer into an array.

So going back to the first posting, the problem persist either way. The question is that if console.log can show the layer name as a string without the indexOf, why do we need to try to convert it to a string per your suggestion?

THANK YOU for your time.

  1. array.forEach(this.map.graphicsLayerIds, function(layerId) {   
  2.           var layer = this.map.getLayer(layerId); 
  3.   if((layer.name).indexOf("Buffer") > -1){ 
  4.   console.log(layer.name); 
  5.   } 
  6.         }, this); 

  1. array.forEach(this.map.graphicsLayerIds, function(layerId) {   
  2.           var layer = this.map.getLayer(layerId); 
  3.   if(layer.name.toString().indexOf("Buffer") > -1){ 
  4.   console.log(layer.name); 
  5.   } 
  6.         }, this); 
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  OK, now I see the issue. You have one or more layers that do not have a name specified in your map, so you need to check for the existence of the name property first:

array.forEach(this.map.graphicsLayerIds, function(layerId) {
          var layer = this.map.getLayer(layerId);
          if(layer.name && layer.name.indexOf("Buffer") > -1){
            console.log(layer.name);
          }
        }, this);