gpt4 book ai didi

javascript - 无法访问具有 1 个主变量的单个变量 [jS]

转载 作者:行者123 更新时间:2023-12-03 05:47:50 25 4
gpt4 key购买 nike

我的结构如下所示:

    var Menu = {
crab : 5,
lobster : 4,
potato : 10,
rib : 3,
wings : 8
}, // foodcount1-4 represent the HTML mapping
foodCount1 = document.getElementById('crab'),
foodCount2 = document.getElementById('lobster'),
foodCount3 = document.getElementById('potato'),
foodCount4 = document.getElementById('rib'),
foodCount5 = document.getElementById('wings');

我不知道如何访问任何单个变量。我尝试过对对象(Menu.i)建立索引,并尝试获取其他变量,例如数组(Menu[1]),但我似乎无法获取它。

编辑:这是我想知道的:

如何访问变量中的其他项目?即我有 var menu = {...}, food1, food2, food3;如何到达食物 1、2 和 3?

最佳答案

如果您只需要访问 Menu 对象的任何数据,只需使用 Menu.crabMenu["crab"] 即可。它是 javascript "associative" array access 的重复项.

关于javascript - 无法访问具有 1 个主变量的单个变量 [jS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40268001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com