简体   繁体   English

如何获取与 javascript 字典关联的值

[英]How to get values associated to a javascript dictionary

I have this declared dictionary variable inside a script tag in a html page.我在 html 页面的脚本标签内有这个声明的字典变量。 I would like to get the values associated to the "Roads" and "Intersections" keys.我想获取与“Roads”和“Intersections”键关联的值。 Theses values change every time the page is refreshed.每次刷新页面时,这些值都会更改。 Catching them, will allow me to change these colors on my python folium map using Javascript :捕捉它们,将允许我使用 Javascript 在我的 python folium 地图上更改这些颜色:

(geo_json_cae0ea33c63e4c438678d293e5c32c0d.setStyle({'fillColor': "#FF0000", 'color': "#FF0000"});

Following suggestions, I tried this but I couldn't have the expected result.根据建议,我尝试了此操作,但无法获得预期的结果。

var layer_control_aec3ac6e0e424b74a19b4c9d1c78ffeb = {
            base_layers : {
            },
            overlays :  {
                "Roads" : geo_json_cae0ea33c63e4c438678d293e5c32c0d,
                "Intersections" : feature_group_1c28eff59e394734be54cf676a09eae1,
            },
        };


window.onload = function() {
  for (var name in this) {
    if (name.includes("layer_control_")) {
      for(let key in this[name]) {
        console.log(key, this[name][key])
        
      }
    }
  }

};

I got lots of things in the console except what I want (the values of overlays dictionary)除了我想要的东西(覆盖字典的值)之外,我在控制台中得到了很多东西

Assuming that you can't access layer_control_aec3ac6e0e424b74a19b4c9d1c78ffeb directly (because the name changes on reload), you can access the variable from within window :假设您无法直接访问layer_control_aec3ac6e0e424b74a19b4c9d1c78ffeb (因为重新加载时名称发生变化),您可以从window访问该变量:

 var layer_control_aec3ac6e0e424b74a19b4c9d1c78ffeb = { base_layers : { }, overlays : { "Roads" : "geo_json_cae0ea33c63e4c438678d293e5c32c0d", "Intersections" : "feature_group_1c28eff59e394734be54cf676a09eae1", }, }; window.onload = function(){ for (const name in this){ if (name.includes("layer_control_")){ let { Roads, Intersections } = window[name].overlays; console.log(Roads, Intersections); // Roads.setStyle({'fillColor': "#FF0000", 'color': "#FF0000"}); // ... } } };

Your initial approach with for (var name in this) was correct.您对for (var name in this)初始方法是正确的。
However, name only contains the name (string) of the variable, and not it's value.但是, name只包含变量的名称(字符串),而不是它的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM