Hi it should be made using arrays but i really cannot do this that way. I get trough json a set of variables like:

--variables i don't need... and then:
"s_title_a": "---",
"s_title_b": "---",
"s_title_c": "---",
 .........

And I need to print all of them with a loop which uses an incrementing char as counter and i have to use this counter as the suffix of the variable name. I tried this:

   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] );
        });

But it doesn't work, all i get is:

undefineda
undefinedb
undefinedc
and so on...

It seems the suffix is added to the variable value and not to the variable name before getting its value. I need help thanks

有帮助吗?

解决方案

Use the following in the callback

console.log( articolo["s_title_" + char] );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top