문제

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