简体   繁体   English

javascript 代理从 object 获取属性

[英]javascript Proxy get property from object

I have javascript array of objects.我有 javascript 对象数组。

I'm using Vue 3 and when trying to retrieve the name by id I don't get a good result我正在使用 Vue 3,当尝试通过id检索name时,我没有得到好的结果

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.我什至不知道它是否重要框架 Vue 3.... 我是纯 javascript。 Apparently a framework makes an object wrapper.显然,一个框架制作了一个 object 包装器。 How to get properties through the proxy object ( result.name )?如何通过代理 object ( result.name ) 获取属性?

Instead of using the proxy directly, try using it as a JSON:与其直接使用代理,不如尝试将其用作 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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