Is there a way to change the integer name to have it use another variable?

Viewed 16

For instance if I were to have two variables: myVariable1 and myVariable2. Could I use some sort of method to say myVariable + 1?

1 Answers

Do you mean this:

const number = 5;
const arrVarNumber = Array(number).fill(0).map((_, index) => index + 1);
const arrVarName = arrVarNumber.map(num => `myVariable${num}`);

console.log(arrVarName); // ["myVariable1","myVariable2","myVariable3","myVariable4","myVariable5"];
const regex = /\d+$/;
function sortByTailNumber(first, second) {
    const num1 = first.match(regex)[0];
    const num2 = second.match(regex)[0];
    return num2 - num1;
}
console.log(arrVarName.sort(sortByTailNumber)); // ["myVariable5","myVariable4","myVariable3","myVariable2","myVariable1"]

Related