Let's assume that this is the proxy.
var target = {};
var p = new Proxy(target, {});
p.a = 1;
p.b = 2;
I know I can access the objects via console.log(p.a) and console.log(p.b) but how do I programatically fetch all the objects stored?
Disclaimer: I'm a Javascript noob but I did read the documentation at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy#Examples but it wasn't really clear.
var target = {};
var p = new Proxy(target, {});
p.a = 1;
p.b = 2;
console.log(p);
// {
// "a": 1,
// "b": 2
// }
console.log(Object.keys(p));
// ["a", "b"]
// only in ES6 and above:
console.log(Object.values(p));
// [1, 2]
// In older JS versions:
for(var key of Object.keys(p)) {
console.log(p[key]);
}
// 1
// 2
What you are looking for is the for .. in loop
for (var property in p) {
console.log(p[property]);
}
More details here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With