0

I have more than 10 javascript variables which change every time the page is refreshed.
I would like to select this variable:

var layer_control_e69649c5e9f941908fcc3173d4dee734 = {
  base_layers: {},
  overlays: {
    "Roads": geo_json_e8253853662b46f985f09f9f27be4df9,
    "Intersections": feature_group_d2005a626dee4cb6ac241e03e5110145,
  },
};

Only layer_control doesnt not change. The string e69649c5e9f941908fcc3173d4dee734 changes every time the page is refreshed. I would like select this variable using the constant string layer_control.

How can I achieve that?

1
  • Why exactly did you tag python? Commented Jun 16, 2021 at 22:02

2 Answers 2

0

Can you tell more about how this variable is created and how you are planing to use it? Without further information's I would just suggest to push the variables directly from the input stream into an array. Then you can just point on the position of the variable instead of knowing the name.

Sign up to request clarification or add additional context in comments.

Comments

0

One method is to use a regex pattern to test() for the variable name in the appropriate scope. Below, I'm filtering the global object (this) for a key that starts with "layout_control_".

var layer_control_e69649c5e9f941908fcc3173d4dee734 = {
  base_layers: {},
  overlays: {
    "Roads": "test",
    "Intersections": "test",
  },
};

var varname = Object.keys(this).filter(
  key => /^layer_control_/.test(key)
)[0];

console.log(varname);
console.log(this[varname] ?? 'n/a');

For reference, see:
Get all Javascript Variables?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.