简体   繁体   中英

javascript Proxy get property from object

I have javascript array of objects.

I'm using Vue 3 and when trying to retrieve the name by id I don't get a good result

const result = this.users.find(item => item.id === 4);
console.log('result', result)

Proxy {id: 4, name: 'john doe', tasks: Array(0)}

But if I do so

console.log('result', result.name)

In the console log I see the following

Cannot read properties of undefined (reading 'name')

I don't even know if it matters framework Vue 3.... My be this is pure javascript. Apparently a framework makes an object wrapper. How to get properties through the proxy object ( result.name )?

Instead of using the proxy directly, try using it as a JSON:

const account = JSON.parse(JSON.stringify(this.users.find(item => item.id === 4)));
console.log(account.name);

or

const account = JSON.stringify(this.users.find(item => item.id === 4));
console.log(account);
console.log(JSON.parse(account).name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM