如何在VUE JavaScript中使用一个变量?
const n = this.prac
const x = this.oa
console.log(x)
console.log(n[0].x)
因此,n存储带有组织,标题等列的JSON文件中的数据。 oa 将具有用户输入的值。我们假设用户输入组织 我想打印n [0] .x ie n [0]。 但是当我做console.log(n [0] .x)
时打印未定义 另一方面,console.log(n [0] .ormanizations)
正常工作。 我在做什么错?我不能使用X代替组织吗?
const n = this.prac
const x = this.oa
console.log(x)
console.log(n[0].x)
So n stores data from a JSON file with columns like Organizations, Title etc.
oa will have a value given as input by the user. Let's suppose user inputs Organizations
I want to print n[0].x i.e n[0].Organizations
but when I do console.log(n[0].x)
it print undefined
On the other hand console.log(n[0].Organizations)
works fine.
What am I doing wrong? Can I not use x instead of Organizations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试从具有变量的对象访问数据时,您可以使用括号符号而不是点符号。
因此,对于您来说,您可以做到
,这应该导致您要寻找的价值。
我在这里找到了这个很棒的答案如何我访问和处理嵌套对象,数组或JSON?提供了更多详细信息和背景。
When attempting to access data from an object with a variable, you can use bracket notation instead of dot notation.
So for you, you can do
Which should result in the value you're looking for.
I found this wonderful answer here How can I access and process nested objects, arrays or JSON? that gives more details and background on why this works.