I'm struggling to use arrays for this task, even though it should be done that way. When I receive a set of variables through JSON, they look something like this:
--variables I don't need... and then:
"s_title_a": "---",
"s_title_b": "---",
"s_title_c": "---",
.........
My challenge is to print all of them using a loop that utilizes an incrementing character as a counter, with the counter serving as the suffix of the variable name. Here's what I attempted:
function charLoop(from, to, callback)
{
var i = from.charCodeAt(0);
var to = to.charCodeAt(0);
for(;i<=to;i++) callback(String.fromCharCode(i));
}
charLoop("a", "l", function(char) {
console.log( window["articolo.s_title_" + char] );
});
Unfortunately, this solution doesn't seem to work as expected. All I see is:
undefineda
undefinedb
undefinedc
and so on...
It appears that the suffix is being added to the variable value instead of the variable name before fetching its value. I would appreciate any help. Thank you.