如何在VUE JavaScript中使用一个变量?

发布于 2025-01-31 21:15:18 字数 339 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2025-02-07 21:15:18

尝试从具有变量的对象访问数据时,您可以使用括号符号而不是点符号

因此,对于您来说,您可以做到

const n = this.prac
const x = this.oa
console.log(x)
console.log(n[0][x])

,这应该导致您要寻找的价值。

我在这里找到了这个很棒的答案如何我访问和处理嵌套对象,数组或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

const n = this.prac
const x = this.oa
console.log(x)
console.log(n[0][x])

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文