3 回答
TA贡献1155条经验 获得超0个赞
我在 Javascript 中有 5 个要填充的数组,我填充的数组将取决于“x”的值。我在这里对变量的使用/可变性感到非常困惑。我的代码如下......
//Node arrays that hold the IDs of each node
nodeOne = [];
nodeTwo = [];
nodeThree = [];
nodeFour = [];
nodeFive = [];
var nodesButtonToNode = {pn_btn_1:"nodeOne", pn_btn_2:"nodeTwo", pn_btn_3:"nodeThree", pn_btn_4:"nodeFour", pn_btn_5:"nodeFive"};
x = "pn_btn_1";
nodesButtonToNode.x.push("I am supposed to go into nodeOne")
TA贡献1876条经验 获得超6个赞
您的nodesButtonToNode应该使用实际的数组而不是它们的名称。
var nodesButtonToNode = {
pn_btn_1: nodeOne,
pn_btn_2: nodeTwo,
pn_btn_3: nodeThree,
pn_btn_4: nodeFour,
pn_btn_5: nodeFive
};
这将让您直接更新数组。
另外, x 的值不在您的映射中,它应该是pn_btn_1
//Node arrays that hold the IDs of each node
nodeOne = [];
nodeTwo = [];
nodeThree = [];
nodeFour = [];
nodeFive = [];
var nodesButtonToNode = {
pn_btn_1: nodeOne,
pn_btn_2: nodeTwo,
pn_btn_3: nodeThree,
pn_btn_4: nodeFour,
pn_btn_5: nodeFive
};
x = "pn_btn_1";
nodesButtonToNode[x].push("I am supposed to go into nodeOne");
console.log(JSON.stringify(nodeOne));
TA贡献1876条经验 获得超5个赞
您的语法有一些错误,请查看示例
const nodeOne = [];
const nodeTwo = [];
const nodeThree = [];
const nodeFour = [];
const nodeFive = [];
const nodesButtonToNode = {
pn_btn_1: nodeOne,
pn_btn_2: nodeTwo,
pn_btn_3: nodeThree,
pn_btn_4: nodeFour,
pn_btn_5: nodeFive
};
const x = "pn_btn_1";
nodesButtonToNode[x].push("I am supposed to go into nodeOne");
console.log(nodesButtonToNode);
添加回答
举报