1

I have 5 arrays in Javascript that I’ll fill and whichever array I fill will depend on the value of “x”. I am getting hopelessly confused regarding variable usage/mutability here. My code is below….

//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")

In a nutshell if x = “pn_btn_1” that would then pull the value of the array that needs to be filled by specifying the key in nodesButtonToNode. In this case that array would be nodeOne. If x = “pn_btn_2” then the area that would need to be added to would be nodeTwo. As expected I am getting lots of undefined errors and I'm not sure where I'm going wrong.

Many TIA for any pointers

Jane Wilkie
  • 1,703
  • 3
  • 25
  • 49
  • Besides there being no key `ptn_btn_1` it is possible by accessing the `window`, yet not very usable. Why dont you make key-array pairs in the object? Like `{pn_btn_1:[], pn_btn_2:[]}` and push it it in there? – Lain Dec 08 '20 at 21:24
  • 1
    You're asking for something between [Dynamically access object property using variable](https://stackoverflow.com/q/4244896) and [“Variable” variables in Javascript?](https://stackoverflow.com/q/5187530). Just use an object to store the arrays and get then using bracket notation. – VLAZ Dec 08 '20 at 21:24

3 Answers3

2

You should use an object with the array names as the keys, then you can access them via bracket obj[var] notation:

//Node arrays that hold the IDs of each node
const nodes = {
  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";

nodes[nodesButtonToNode[x]].push("I am supposed to go into nodeOne");

console.log(nodes);
Nick
  • 138,499
  • 22
  • 57
  • 95
1

Your nodesButtonToNode should use the actual arrays rather than their names.

var nodesButtonToNode = {
    pn_btn_1: nodeOne,
    pn_btn_2: nodeTwo,
    pn_btn_3: nodeThree,
    pn_btn_4: nodeFour,
    pn_btn_5: nodeFive
};

This will let you update the arrays directly.

Also, the value of x isn't in your mapping it should be 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));
phuzi
  • 12,078
  • 3
  • 26
  • 50
1

You have some errors in your syntax, check out example

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);
tarkh
  • 2,424
  • 1
  • 9
  • 12