I need to merge the attributes on a leaflet geojson layer and another geojson based on a common field. I have tried the following code, which does put the keys in the layer but all values are undefined. How do I also get the value to transfer to the layer. geojson is the variable that holds the attributes that I want merged in the layer.

var schemaL = new L.GeoJSON(schema);

// get RG attributes
schemaL.eachLayer(function (layer) {
  for (var i = 0; i < geojson.features.length; i++) {
    if (layer.feature.properties.CodeLink === geojson.features[i].properties.SiteId) {
      for (var key in geojson.features[i].properties) {
        layer.feature.properties[key] = geojson[key];
      }
    }
  }
});
up vote 0 down vote accepted

Ok.. played around a bit and found the solution. The geojson[key] statement gets the key only. I had to add the following line add the end to the code to get and transfer the value.

  layer.feature.properties[key] = geojson.features[i].properties[key];

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.