Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reliably convert a proxy to a standard object?

I'm working with events in JavaScript, and often the event is a proxy object. When debugging I can usually use this to get a copy of the object.

console.log(JSON.parse(JSON.stringify(event.detail)));

Sometimes (today, a few times), this outputs a proxy object again, but if I don't parse it, I get the raw string with the information I need:

console.log(JSON.stringify(event.detail));

Why is JSON.parse turning a string into a proxy object? How can I get a copy of the embedded object every time?

like image 945
Chuck Avatar asked Oct 18 '25 06:10

Chuck


1 Answers

I've found that using the spread operator can do a shallow clone of a Proxy.

let proxy = new Proxy({ foo: 1, bar: 2 }, {});
let cloned = { ... proxy };

console.log(cloned);

In your case, you could try { ... event.detail } to get a shallow clone of the object.

like image 85
Calvin Furano Avatar answered Oct 20 '25 00:10

Calvin Furano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!