0

I am trying to access dynamic variable in js. This is how I am creating dynamic variable:

var marker{{$working_order->user_id}} = new google.maps.Marker({
                position: address,
                map: map,
            });

after that when any event happen I want to call that variable again like this:

console.log('marker'+data.user_id)

But this time it is not variable it is just a string is there any way I can access it as variable?

2
  • Maybe keep it in an object or map keyed by user id? Commented Apr 17, 2021 at 12:55
  • Don't create dynamic variables. Use an object literal with dynamic property names, or even a Map, instead. Commented Apr 17, 2021 at 16:00

1 Answer 1

1
const marker = {};

marker[$working_order->user_id] = new google.maps.Marker({
                position: address,
                map: map,
            });

console.log(marker[$working_order->user_id]);
Sign up to request clarification or add additional context in comments.

1 Comment

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. You can use the edit button to improve this answer to get more votes and reputation!

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.