1

I have property yaCounter27352058 in window object. I can easily get it using bracket notation

window["yaCounter27352058"]

The problem is that I don't know object id, so in general I want to get all objects like this

window["yaCounter*"]

2 Answers 2

4

You can query based of Object.keys:

var values = Object.keys(window).filter(function(el) {
    return /^yaCounter.*?/i.test(el);
});

Then you can iterate:

values.forEach(function(key) {
   console.log(key, window[key]); 
});
Sign up to request clarification or add additional context in comments.

Comments

0

Well, in fact you can't. But you can do something else.

You can try to list all your properties with

var properties = Object.keys(window).

Then with a regexp you will select your properties starting with yaCounter :

var reg = new RegExp("^yaCounter.*");
var goodProp = [];
properties.forEach(function(prop) {
    if (reg.exec(prop) != null)
        goodProp.push(prop);
}

And them use these with :

goodProp.forEach(function(val) {
    window[val];
}));

2 Comments

exec only takes a string, so this would only match if the yaCounter variable was the first property on window which it's unlikely to be...
Yes you are right, exec should be called for each item of the properties array.

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.