I'm attempting to dynamically generate an array from another array in JavaScript. The task at hand involves taking a string that represents a mathematical literal expression, such as '2a + 3b + 4a + 5c', and splitting it into an array containing only the literal parts of the equation (e.g., 'a,b,a,c').
My initial approach was to use the following code snippet:
var expression = '2a + 3b + 4a + 5c';
var NumbersArray = expression.split(' + '); /* NumbersArray = 2a,3b,4a,5c */
alert('So far it's working!');
var LettersArray = new Array();
for (var i = 0; i < NumbersArray.length; i++) {
eval('var LettersArray[' + i + '] = NumbersArray[' + i + '].replace(/[0-9]/g,"");');
alert(eval('LettersArray[' + i + ']'));
}
Despite my efforts, the code didn't produce the desired outcome. How can I rectify this issue?