Home > Enterprise >  How to access object with a randomly chosen name stored in a variable?
How to access object with a randomly chosen name stored in a variable?

Time:03-28

I've got 16 objects with names like: aBoard, bBoard, cBoard and so on, eg. let aBoard = { currentValue: 0, valuesHistory: [], ID: "unitA", borderValues: [1, 0, 0, 1], free: true };

I have an array of corresponding names, randomly chose one of them and change it so it matches the name of one of the objects.

const BOARDARRAY = ["unitA", "unitB", "unitC", "unitD", "unitE", "unitF", "unitG", "unitH", "unitI", "unitJ", "unitK", "unitL", "unitM", "unitN", "unitO", "unitP"];

let randomizer = Math.floor(Math.random()*16);
currentBoardTile = BOARDARRAY[randomizer];
let temp = (currentBoardTile.charAt(currentBoardTile.length -1).toLowerCase());
JSObjectBoardUnit = (temp   "Board");

How to access the object using my JSObjectBoardUnit? In other words, how to make JS "understand" that I want to treat JSObjectBoardUnit value (string) as a value of the object address?

Eg. Let's day JSObjectBoardUnit = aBoard; Basically the outcome I want is: aBoard.key1 = JSObjectBoardUnit.key1. I'd love to use the value stored in JSObjectBoardUnit to access the name of the predefined object aBoard.

CodePudding user response:

I'm not sure to understand well your question but I think this 2 methode could maybe help you.

You can access attribute of a object with a string by using

const obj = {toto: 1};
const name = "toto";
console.log(obj["toto"]); // 1
console.log(obj[name]); // here we use variable and the result is 1

so you could store all yoyr object inside one and do this.

const allBoard = {
"aboard": null, // do not put null use your board
}

console.log(allBoard[(temp   "board")]); // display the temp   board attribute so if temps is equal to a then it will get the aboard

this is what you want, getting object from a string.

But I saw that the aboard also have an id attribute with "unitA" Instead you could build an array of aboard, bboard ....

and use the Array.find() methode that will return the object that match the condition.

in your case

const myBoardArray = [{ currentValue: 0, valuesHistory: [], ID: "unitA", borderValues: [1, 0, 0, 1], free: true }, ....];
let randomizer = Math.floor(Math.random()*16);
currentBoardTile = BOARDARRAY[randomizer];

myBoardArray.find((board) => board.ID === currentBoardTile);

CodePudding user response:

2 options

  1. Put the boards in a list, and iterate over them with a for loop. In the for loop, use an if statement to see which Id matches the board you want.
let boards = [aBoard , bBoard, cBoard];
boards.forEach(board=> {
    if (board.ID == currentBoardTile) {
        //do something
    }
});
  1. Create a dictionary where the key is the board id and the respective object is the value. Use the board id to get the respective value.
var boards = { 
    "unitA" : boardA, 
    "unitB" : boardB, 
    .... 
}; 
currentBoardTile = BOARDARRAY[randomizer]; 
console.log(currentBoardTile   " : "   boards[currentBoardTile]);
  • Related