I have a problem when I\m trying to convert a string to a variable in an array.
The variables are:
const n = 1; const s = -1;
The array string:
let walk = ['n', 's', 'n', 's', 'n', 's', 'n', 's', 'n', 'n'];
I want to convert automatically to this variable array:
let walk = [n, s, n, s, n, s, n, s, n, n];
I’m trying to split but the array still string not a var:
let text = walks.toString().split(',').join('-')
console.log(text)
>Solution :
You could put s and n into an object.
const variables = {
n: 1,
s: -1
};
let walk = ['n', 's', 'n', 's', 'n', 's', 'n', 's', 'n', 'n'];
console.log(
walk.map(variable => variables[variable])
)
See this question for more info on accessing variables via another variable.